Python provides powerful built-in data structures. This chapter covers lists in detail, plus tuples, sets, and dictionaries.
More on Lists
Lists have many useful methods:
| Method | Description |
|---|
list.append(x) | Add an item to the end |
list.extend(iterable) | Extend the list by appending all items from the iterable |
list.insert(i, x) | Insert an item at position i |
list.remove(x) | Remove the first item whose value equals x |
list.pop([i]) | Remove and return item at position i (default: last item) |
list.clear() | Remove all items |
list.index(x) | Return index of first occurrence of x |
list.count(x) | Return number of times x appears |
list.sort() | Sort the items in place |
list.reverse() | Reverse the elements in place |
list.copy() | Return a shallow copy |
Example Usage
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
>>> fruits.count('apple')
2
>>> fruits.index('banana')
3
>>> fruits.index('banana', 4) # Find next banana starting at position 4
6
>>> fruits.reverse()
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
>>> fruits.append('grape')
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
>>> fruits.sort()
>>> fruits
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
>>> fruits.pop()
'pear'
Methods like insert, remove, and sort that only modify the list return None - this is a design principle for all mutable data structures in Python.
Using Lists as Stacks
Lists work well as stacks (last-in, first-out):
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
Using Lists as Queues
Lists are not efficient for queues (first-in, first-out) because inserts/pops from the beginning are slow. Use collections.deque instead:
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry") # Terry arrives
>>> queue.append("Graham") # Graham arrives
>>> queue.popleft() # The first to arrive now leaves
'Eric'
>>> queue.popleft() # The second to arrive now leaves
'John'
>>> queue # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])
List Comprehensions
List comprehensions provide a concise way to create lists:
>>> squares = [x**2 for x in range(10)]
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
This is equivalent to:
>>> squares = []
>>> for x in range(10):
... squares.append(x**2)
Complex List Comprehensions
Combine elements from two lists:
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
More examples:
>>> vec = [-4, -2, 0, 2, 4]
>>> [x*2 for x in vec] # create a new list with values doubled
[-8, -4, 0, 4, 8]
>>> [x for x in vec if x >= 0] # filter to exclude negatives
[0, 2, 4]
>>> [abs(x) for x in vec] # apply a function to all elements
[4, 2, 0, 2, 4]
Nested List Comprehensions
Transpose a matrix:
>>> matrix = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... ]
>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
For complex operations, prefer built-in functions like zip():>>> list(zip(*matrix))
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
The del Statement
Remove items from a list by index:
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]
del can also delete entire variables:
Tuples and Sequences
A tuple consists of values separated by commas:
>>> t = 12345, 54321, 'hello!'
>>> t[0]
12345
>>> t
(12345, 54321, 'hello!')
>>> # Tuples may be nested:
>>> u = t, (1, 2, 3, 4, 5)
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
Tuples are immutable:>>> t[0] = 88888
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
But they can contain mutable objects:>>> v = ([1, 2, 3], [3, 2, 1])
>>> v
([1, 2, 3], [3, 2, 1])
Empty and Single-Item Tuples
>>> empty = ()
>>> singleton = 'hello', # note trailing comma
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)
Tuple Packing and Unpacking
>>> t = 12345, 54321, 'hello!' # tuple packing
>>> x, y, z = t # sequence unpacking
Sets
A set is an unordered collection with no duplicates:
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket) # duplicates removed
{'orange', 'banana', 'pear', 'apple'}
>>> 'orange' in basket # fast membership testing
True
Set Operations
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b # letters in both a and b
{'a', 'c'}
>>> a ^ b # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
Set Comprehensions
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
{'r', 'd'}
Dictionaries
Dictionaries are indexed by keys (any immutable type):
>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'jack': 4098, 'sape': 4139, 'guido': 4127}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'jack': 4098, 'guido': 4127, 'irv': 4127}
>>> list(tel)
['jack', 'guido', 'irv']
>>> sorted(tel)
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
Creating Dictionaries
From sequences:
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'guido': 4127, 'jack': 4098}
With comprehensions:
>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}
With keyword arguments:
>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'guido': 4127, 'jack': 4098}
Looping Techniques
Looping Through Dictionaries
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
... print(k, v)
...
gallahad the pure
robin the brave
Looping with Index
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
... print(i, v)
...
0 tic
1 tac
2 toe
Looping Over Multiple Sequences
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print('What is your {0}? It is {1}.'.format(q, a))
...
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
Looping in Reverse
>>> for i in reversed(range(1, 10, 2)):
... print(i)
...
9
7
5
3
1
Looping in Sorted Order
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
... print(f)
...
apple
banana
orange
pear
More on Conditions
Comparison operators can be chained:
>>> a < b == c # tests whether a < b and b == c
Boolean operators and, or, and not:
>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
>>> non_null = string1 or string2 or string3
>>> non_null
'Trondheim'
Boolean operators are short-circuit: they stop evaluating as soon as the outcome is determined.
Comparing Sequences
Sequences are compared using lexicographical ordering:
(1, 2, 3) < (1, 2, 4)
[1, 2, 3] < [1, 2, 4]
'ABC' < 'C' < 'Pascal' < 'Python'
(1, 2, 3, 4) < (1, 2, 4)
(1, 2) < (1, 2, -1)
(1, 2, 3) == (1.0, 2.0, 3.0)
Next Steps
You now understand Python’s core data structures. Next, learn how to organize code into reusable Modules.