Lists Thinkpython 2
Lists Thinkpython 2
>>> numbers[1] = 5
LISTS >>> numbers[42, 5]
TRAVERSING A LIST
The most common way to traverse the elements of a
list is with a for loop. The syntax is the same as for
strings:
for cheese in cheeses:
print(cheese)
Figure 7.1. State diagram. This works well if you only need to read the elements
of the list. But if you want to write or update the
elements, you need the indices. A common way to do
LIST ARE MUTABLE that is to combine the built-in functions range and
len:
The syntax for accessing the elements of a list is the
for i in range(len(numbers)):
same as for accessing the charactersof a string—the
bracket operator. The expression inside the brackets numbers[i] = numbers[i] * 2
specifies the index. Remember that the indices start at This loop traverses the list and updates each element.
0: len returns the number of elements in the list.
>>> cheeses[0] range returns a list of indices from 0 to n−1, where n
'Cheddar'
is the length of the list. Each time through the loop i
Unlike strings, lists are mutable. When the bracket gets the index of the next element. The assignment
operator appears on the left side of anassignment, it statement in the body uses i to read the old value of
the element and to assign the new value.
identifies the element of the list that will be assigned.
A for loop over an empty list never runs the body: >>> t = ['a','b','c']
>>> t.append('d')
for x in []: >>> t
['a','b','c','d']
print('This never happens.')
Although a list can contain another list, the nested list extend takes a list as an argument and appends all of
still counts as a single element. The length of this list the elements:
is four: >>> t1 = ['a','b','c']
['spam', 1, ['Brie', 'Roquefort', 'Pol le >>> t2 = ['d','e']
Veq'], [1, 2, 3]] >>> t1.extend(t2)
>>> t1
['a','b','c','d','e']
LIST OPERATIONS
This example leaves t2 unmodified.
The + operator concatenates lists:
>>> a = [1, 2, 3]>>> b = [4, 5, 6] sort arranges the elements of the list from low to
>>> c = a + b high:
>>> c[1, 2, 3, 4, 5, 6]
The * operator repeats a list a given number of times: >>> t = ['d','c','e','b','a']
>>> [0] * 4[0, 0, 0, 0] >>> t.sort()
>>> [1, 2, 3] * 3[1, 2, 3, 1, 2, 3, 1, >>> t
2, 3] ['a','b','c','d','e']
The first example repeats [0] four times. The second
Most list methods are void; they modify the list and
example repeats the list [1, 2, 3] three times.
return None.
ALIASING
OBJECTS AND VALUES
If a refers to an object and you assign b = a, then
If we run these assignment statements:
both variables refer to the same object:
a ='banana'
>>> a = [1, 2, 3]
b ='banana'
>>> b = a
>>> b is a
We know that a and b both refer to a string, but we True
don’t know whether they refer to the same string.
There are two possible states, shown in Figure 7.2. The state diagram looks like Figure 7.4. The
In one case, a and b refer to two different objects that association of a variable with an object is called a
have the same value. In the second case, they refer to reference. In this example, there are two references to
the same object. the same object. An object with more than one
reference has more than one name, so we say that the
To check whether two variables refer to the same object is aliased. If the aliased object is mutable,
object, you can use the is operator. changes made with one alias affect the other:
>>> b[0] = 42
>>> a
Figure 7.4. State diagram. [42, 2, 3]
GLOSSARY
list: A sequence of values.
element: One of the values in a list (or other
sequence), also called items.
nested list: A list that is an element of another list.
accumulator: A variable used in a loop to add up or
accumulate a result.
augmented assignment: A statement that updates the
value of a variable using an operator like +=.
reduce: A processing pattern that traverses a
sequence and accumulates the elements into a single
result.
map: A processing pattern that traverses a sequence
and performs an operation on eachelement.
filter: A processing pattern that traverses a list and
selects the elements that satisfy somecriterion.
object: Something a variable can refer to. An object
has a type and a value.
equivalent: Having the same value.
identical: Being the same object (which implies
equivalence).
reference: The association between a variable and its
value.
aliasing: A circumstance where two or more variables
refer to the same object.
delimiter: A character or string used to indicate
where a string should be split.