Lists
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam
['cat', 'bat', 'rat', 'elephant']
Getting Individual Values in a List with
Indexes >>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[0]
'cat'
>>> spam[1]
'bat'
>>> spam[2]
'rat'
>>> spam[3]
'elephant'
Negative Indexes
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[-1]
'elephant'
>>> spam[-3]
'bat'
>>> 'The {} is afraid of the {}.'.format(spam[-1],
spam[-3]) 'The elephant is afraid of the bat.'
Getting Sublists with Slices
>>> spam = ['cat', 'bat', 'rat',
'elephant'] >>> spam[0:4]
['cat', 'bat', 'rat', 'elephant']
>>> spam[1:3]
['bat', 'rat']
>>> spam[0:-1]
['cat', 'bat', 'rat']
>>> spam = ['cat', 'bat', 'rat',
'elephant'] >>> spam[:2]
['cat', 'bat']
>>> spam[1:]
['bat', 'rat', 'elephant']
Slicing the complete list will perform a copy:
>>> spam2 = spam[:]
['cat', 'bat', 'rat', 'elephant']
>>> spam.append('dog')
>>> spam
['cat', 'bat', 'rat', 'elephant',
'dog'] >>> spam2
['cat', 'bat', 'rat', 'elephant']
Getting a List’s Length with
len() >>> spam = ['cat', 'dog',
'moose'] >>> len(spam)
3
Changing Values in a List with
Indexes >>> spam = ['cat', 'bat', 'rat',
'elephant'] >>> spam[1] = 'aardvark'
>>> spam
['cat', 'aardvark', 'rat',
'elephant'] >>> spam[2] = spam[1]
>>> spam
['cat', 'aardvark', 'aardvark',
'elephant'] >>> spam[-1] = 12345
>>> spam
['cat', 'aardvark', 'aardvark', 12345]
List Concatenation and List
Replication >>> [1, 2, 3] + ['A', 'B', 'C']
[1, 2, 3, 'A', 'B', 'C']
>>> ['X', 'Y', 'Z'] * 3
['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y',
'Z'] >>> spam = [1, 2, 3]
>>> spam = spam + ['A', 'B', 'C']
>>> spam
[1, 2, 3, 'A', 'B', 'C']
Removing Values from Lists with del
Statements >>> spam = ['cat', 'bat', 'rat',
'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat']
Using for Loops with Lists
>>> supplies = ['pens', 'staplers', 'flame-throwers',
'binders'] >>> for i, supply in enumerate(supplies):
>>> print('Index {} in supplies is: {}'.format(str(i),
supply))
Index 0 in supplies is: pens
Index 1 in supplies is: staplers
Index 2 in supplies is: flame-throwers
Index 3 in supplies is: binders
Looping Through Multiple Lists with
zip() >>> name = ['Pete', 'John', 'Elizabeth']
>>> age = [6, 23, 44]
>>> for n, a in zip(name, age):
>>> print('{} is {} years old'.format(n, a))
Pete is 6 years old
John is 23 years old
Elizabeth is 44 years old
The in and not in Operators
>>> 'howdy' in ['hello', 'hi', 'howdy',
'heyas'] True
>>> spam = ['hello', 'hi', 'howdy', 'heyas']
>>> 'cat' in spam
False
>>> 'howdy' not in spam
False
>>> 'cat' not in spam
True
The Multiple Assignment Trick
The multiple assignment trick is a shortcut that lets you assign multiple variables with
the values in a list in one line of code. So instead of doing this:
>>> cat = ['fat', 'orange', 'loud']
>>> size = cat[0]
>>> color = cat[1]
>>> disposition = cat[2]
You could type this line of code:
>>> cat = ['fat', 'orange', 'loud']
>>> size, color, disposition = cat
The multiple assignment trick can also be used to swap the values in two
variables: >>> a, b = 'Alice', 'Bob'
>>> a, b = b, a
>>> print(a)
'Bob'
>>> print(b)
'Alice'
Augmented Assignment Operators
spam *= 1
Operator
spam /= 1
spam += 1
spam %= 1
spam -= 1
Examples:
>>> spam = 'Hello' spam = spam + 1 spam = spam
>>> spam += ' world!'
- 1 spam = spam * 1 spam =
>>> spam
spam / 1 spam = spam % 1
'Hello world!'
Equivalent
>>> bacon = ['Zophie']
>>> bacon *= 3
>>> bacon
['Zophie', 'Zophie', 'Zophie']
Finding a Value in a List with the index()
Method >>> spam = ['Zophie', 'Pooka', 'Fat-tail',
'Pooka']
>>> spam.index('Pooka')
Adding Values to Lists with the append() and insert()
Methods
append():
>>> spam = ['cat', 'dog', 'bat']
>>> spam.append('moose')
>>> spam
['cat', 'dog', 'bat', 'moose']
insert():
>>> spam = ['cat', 'dog', 'bat']
>>> spam.insert(1, 'chicken')
>>> spam
['cat', 'chicken', 'dog', 'bat']
Removing Values from Lists with
remove() >>> spam = ['cat', 'bat', 'rat',
'elephant']
>>> spam.remove('bat')
>>> spam
['cat', 'rat', 'elephant']
If the value appears multiple times in the list, only the first instance of the value will be
removed.
Sorting the Values in a List with the sort()
Method >>> spam = [2, 5, 3.14, 1, -7]
>>> spam.sort()
>>> spam
[-7, 1, 2, 3.14, 5]
>>> spam = ['ants', 'cats', 'dogs', 'badgers',
'elephants'] >>> spam.sort()
>>> spam
['ants', 'badgers', 'cats', 'dogs', 'elephants']
You can also pass True for the reverse keyword argument to have sort() sort the
values in reverse order:
>>> spam.sort(reverse=True)
>>> spam
['elephants', 'dogs', 'cats', 'badgers', 'ants']
If you need to sort the values in regular alphabetical order, pass str. lower for the key
keyword argument in the sort() method call:
>>> spam = ['a', 'z', 'A', 'Z']
>>> spam.sort(key=str.lower)
>>> spam
['a', 'A', 'z', 'Z']
You can use the built-in function sorted to return a new list:
>>> spam = ['ants', 'cats', 'dogs', 'badgers',
'elephants'] >>> sorted(spam)
['ants', 'badgers', 'cats', 'dogs', 'elephants']
Tuple Data Type
>>> eggs = ('hello', 42, 0.5)
>>> eggs[0]
'hello'
>>> eggs[1:3]
(42, 0.5)
>>> len(eggs)
The main way that tuples are different from lists is that tuples, like strings, are
immutable.
Converting Types with the list() and tuple()
Functions >>> tuple(['cat', 'dog', 5])
('cat', 'dog', 5)
>>> list(('cat', 'dog', 5))
['cat', 'dog', 5]
>>> list('hello')
['h', 'e', 'l', 'l', 'o']