Lists


 
1
vocabulary = ["iteration", "selection", "control"]
2
numbers = [17, 123]
3
empty = []
4
mixedlist = ["hello", 2.0, 5*2, [10, 20]]
5
6
print(numbers)
7
print(mixedlist)
8
newlist = [ numbers, vocabulary ]
9
print(newlist)
10


(chp09_01)


4
 
1
alist =  ["hello", 2.0, 5, [10, 20]]
2
print(len(alist))
3
print(len(['spam!', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]))
4


(chp09_01a)


6
 
1
numbers = [17, 123, 87, 34, 66, 8398, 44]
2
print(numbers[2])
3
print(numbers[9 - 8])
4
print(numbers[-2])
5
print(numbers[len(numbers) - 1])
6


(chp09_02)


5
 
1
fruit = ["apple", "orange", "banana", "cherry"]
2
3
print("apple" in fruit)
4
print("pear" in fruit)
5


(chp09_4)


16
 
1
fruit = ["apple", "orange", "banana", "cherry"]
2
print([1, 2] + [3, 4])
3
print(fruit + [6, 7, 8, 9])
4
5
print([0] * 4)
6
print([1, 2, ["hello", "goodbye"]] * 2)
7
8
numlist = [6, 7]
9
10
newlist = fruit + numlist
11
12
zeros = [0] * 4
13
14
print(newlist)
15
print(zeros)
16


(chp09_5)


6
 
1
a_list = ['a', 'b', 'c', 'd', 'e', 'f']
2
print(a_list[1:3])
3
print(a_list[:4])
4
print(a_list[3:])
5
print(a_list[:])
6


(chp09_6)

Lists are Mutable

  • Unlike strings, lists are mutable.
  • This means we can change an item in a list by accessing it directly as part of the assignment statement.
  • Using the indexing operator (square brackets) on the left side of an assignment, we can update one of the list items.

9
 
1
bananavariable = "banana"
2
#bananavariable[O]= 'c'
3
fruit = ["banana", "apple", "cherry"]
4
print(fruit)
5
6
fruit[0] = "pear"
7
fruit[-1] = "orange"
8
print(fruit)
9


(ch09_7)

List Deletion

  • Using slices to delete list elements can be awkward and therefore error-prone.
  • The del statement removes an element from a list by using its position.

8
 
1
a = ['one', 'two', 'three']
2
del a[1]
3
print(a)
4
5
alist = ['a', 'b', 'c', 'd', 'e', 'f']
6
del alist[1:5]
7
print(alist)
8


(ch09_11)

As you might expect, del handles negative indices and causes a runtime error if the index is out of range.

Nested Lists

  • A nested list is a list that appears as an element in another list.
  • To extract an element from the nested list, we can proceed in two steps.
  • First, extract the nested list, then extract the item of interest.
  • It is also possible to combine those steps using bracket operators that evaluate from left to right.

8
 
1
nested = ["hello", 2.0, 5, [10, 20]]
2
innerlist = nested[3]
3
print(innerlist)
4
item = innerlist[1]
5
print(item)
6
7
print(nested[3][1])
8


(chp09_nest)

Split and Join

The split method breaks a string into a list of words. By default, any number of whitespace characters is considered a word boundary.


4
 
1
song = "The rain in Spain..."
2
wds = song.split()
3
print(wds)
4


(ch09_split1)

An optional argument called a delimiter can be used to specify which characters to use as word boundaries. The following example uses the string ai as the delimiter:


4
 
1
song = "The rain in Spain..."
2
wds = song.split('ai')
3
print(wds)
4


(ch09_split2)

Notice that the delimiter doesn’t appear in the result.

The inverse of the split method is join. You choose a desired separator string, (often called the glue) and join the list with the glue between each of the elements.


9
 
1
wds = ["red", "blue", "green"]
2
glue = ';'
3
s = glue.join(wds)
4
print(s)
5
print(wds)
6
7
print("***".join(wds))
8
print("".join(wds))
9


(ch09_join)

The list that you glue together (wds in this example) is not modified. Also, you can use empty glue or multi-character strings as glue.

list Type Conversion Function

Python has a built-in type conversion function called list that tries to turn whatever you give it into a list. For example, try the following:


3
 
1
xs = list("Crunchy Frog")
2
print(xs)
3


(ch09_list1)

Lists and for loops

It is also possible to perform list traversal using iteration by item as well as iteration by index.


5
 
1
fruits = ["apple", "orange", "banana", "cherry"]
2
3
for afruit in fruits:     # by item
4
    print(afruit)
5


(chp09_03a)

We can also use the indices to access the items in an iterative fashion.


5
 
1
fruits = ["apple", "orange", "banana", "cherry"]
2
3
for position in range(len(fruits)):     # by index
4
    print(fruits[position])
5


(chp09_03b)

Next Section - Objects and References