## Lists

### Preface

- I expect this letter to be augmented as we learn more about lists

- A description of important list module components is available. Your are
advised to check it out.*strongly*

### Basics

- A Python list is an
*ordered list*; that is, there is a first element, second element, and so on.

- Like as it is with strings, the first element of a list had index 0, and the last element of a list has index
`n-1`

where`n`

is the number of elements in the list.

- A new empty list can be created using empty brackets. The statement

stuff = []

makes

`stuff`

a list with no elements.

- A new list can be created listing values within brackets. The statement

stuff = [ 'abc', 1112, math.pi, ]

initializes

`stuff`

to be a three-element list, the first element has the string`'abc'`

as its value, the second element has value 1112, and the last element has value`math.pi`

.

- The
`in`

and`not in`

operations can be used to determine whether a value is or is not an element of a list. Suppose`stuff`

is a list, then

b1 = (x in stuff )

b2 = (x not in stuff )

sets

`b1`

to be`True`

or`False`

depending whether`x`

is an element of`stuff`

, and sets`b2`

to be`True`

or`False`

depending whether`x`

is not an element of`stuff`

.

- The
`[]`

(brackets) operator works on lists in the same way as it does on strings.

- If there is just a single integer with the brackets, then the integer is used to access a single element from the list. Suppose
`stuff`

is a list, then`stuff[ i ]`

is the element into the list whose index is`i`

.

- If there is a
`:`

within the brackets then anewslice of the list is gotten. Suppose`stuff`

is a list, then`stuff[ i : j ]`

is list whose elements are equal to those of`stuff[ i ]`

,`stuff[ i + 1]`

, ...`stuff[ j-1 ]`

. Like it is with strings, the`i`

and`j`

are optional. If`i`

is omitted, then 0 is used in its stead; if`j`

is omitted, then the length of the list is used in its stead.

- The built-in function
`len()`

when given a list as its argument, returns the number of elements in the list. Suppose`stuff`

is a list, then

n = len( stuff )

sets the

`n`

to be the number of elements in`stuff`

.

- Built-in functions
`max()`

and`min()`

can be used to determine the largest and smallest value in a list. Suppose`stuff`

is a list, then

largest = max( stuff )

smallest = min( stuff )

sets

`largest`

and`smallest`

respectively to the maximum and minimum value in`stuff`

.

- Unlike strings whose character sequences are immutable once created, lists are mutable — the values of individual elements can change, and elements can be deleted, inserted, or added.

- Like strings, lists have many member functions that can inspect and manipulate the list.

- List member function
`append()`

appends an element to the end of its list. Suppose`stuff`

is a list, then

stuff.append( v )

adds

`v`

to the end of list`stuff`

.

- An element value can be changed using assignment. Suppose
`stuff`

is a list, then

stuff[ x ] = v

sets the

`x`

^{th}element of`stuff`

to have value`v`

- An expanded but still partial description of the list module is available. It indicates that there are member functions for adding, removing, inserting, counting, and searching a list.

### Patterns

- Computer scientists like the use the word
*pattern*to mean a template for an algorithm. Three patterns are listed below.

- Suppose you want to sum the elements of a list. An
*accumulator*is necessary. The accumulator keeps a running total of the elements considered so far. A summing accumulator is typically initialized to 0 (the running total before any elements have been considered). Suppose`numbers`

is a numeric list, then code segment

total = 0

for nbr in numbers :

total = total + nbr

print( total )

prints the sum of the elements in

`numbers`

.

- Suppose
`inputs`

is a list of integer*numeric strings*. A new list whose elements are the numeric counterparts of those strings can be gotten by considering each element of`inputs`

and adding its conversion to the new list in turn. The new list before any of`inputs`

has been considered should be initialized to an empty list (i.e.,`[]`

). The conversions can then be accumulated to produce the new list.

numbers = []

for s in inputs :

nbr = int( s )

numbers.append( nbr)

print( numbers )

prints the

`inputs`

as a list of integers.

- Suppose
`numbers`

is a list of integers and what we want to do is to update the list so that all values are positive.*The following code does not work.*

for nbr in numbers :

`nbr = abs( nbr )`

Why you say? What the code does is repeatedly set

`nbr`

to be the absolute value of its current value. Doing so does not change`numbers`

. However,the following code does work.

n = len( numbers )

for i in range( 0, n ) :

`numbers[ i ] = abs( numbers[ i ] )`

Why you say again? Because each iteration another

list element ofto be the absolute value of its currrent value.`numbers`

is updated

- This next pattern really belongs with patterns for strings, but what the heck. A new string can be gotten through accumulation. The new string is initialized to the empty string (i.e.,
`''`

). Characters are added to the new string through an accumulation process.

new_string = ''

for s in stuff

c = ... # depends on what we want out of s

new_string = new_string + c

print( new_string )

prints the new string.