Data Structures

This chapter describes some things you’ve learned about already in more detail, and adds some new things as well.

More on Lists

  • list.append(x)

    • Add an item to the end of the list; equivalent to a[len(a):] = [x].
  • list.extend(L)

    • Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
  • list.insert(i, x)

    • Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
  • list.remove(x)

    • Remove the first item from the list whose value is x. It is an error if there is no such item.
  • list.pop([i])

    • Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
  • list.index(x)

    • Return the index in the list of the first item whose value is x. It is an error if there is no such item.
  • list.count(x)

    • Return the number of times x appears in the list.
  • list.sort(cmp=None, key=None, reverse=False)

    • Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).
  • list.reverse()

    • Reverse the elements of the list, in place.

In [71]:
from __future__ import print_function
a = [66.25, 333, 333, 1, 1234.5]
print(a.count(333), a.count(66.25), a.count('x'))


2 1 0

In [2]:
a.insert(2, -1)
a.append(333)
a


Out[2]:
[66.25, 333, -1, 333, 1, 1234.5, 333]

In [3]:
a.index(333)


Out[3]:
1

In [4]:
a.remove(333)
a


Out[4]:
[66.25, -1, 333, 1, 1234.5, 333]

In [5]:
a.reverse()
a


Out[5]:
[333, 1234.5, 1, 333, -1, 66.25]

In [6]:
a.sort()
a


Out[6]:
[-1, 1, 66.25, 333, 333, 1234.5]

In [7]:
a.pop()


Out[7]:
1234.5

In [8]:
a


Out[8]:
[-1, 1, 66.25, 333, 333]

You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed – they return the default None.

This is a design principle for all mutable data structures in Python.

Using Lists as Stacks

The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”).

To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index. For example:


In [9]:
stack = [3, 4, 5]
stack.append(6)
stack.append(7)
stack


Out[9]:
[3, 4, 5, 6, 7]

In [10]:
stack.pop()


Out[10]:
7

In [11]:
stack


Out[11]:
[3, 4, 5, 6]

In [12]:
stack.pop()
stack.pop()
stack


Out[12]:
[3, 4]

Using Lists as Queues

It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).

To implement a queue, use collections.deque which was designed to have fast appends and pops from both ends. For example:


In [13]:
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


Out[13]:
'Eric'

In [14]:
queue.popleft()                 # The second to arrive now leaves


Out[14]:
'John'

In [15]:
queue                           # Remaining queue in order of arrival


Out[15]:
deque(['Michael', 'Terry', 'Graham'])

List Comprehension

List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.

For example, assume we want to create a list of squares, like:


In [16]:
squares = []
for x in range(10):
    squares.append(x**2)

squares


Out[16]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

We can obtain the same result with:


In [17]:
squares = [x**2 for x in range(10)]
squares


Out[17]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

This is also equivalent to squares = map(lambda x: x**2, range(10)), but it’s more concise and readable.

A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses.

The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.

For example, this listcomp combines the elements of two lists if they are not equal:


In [18]:
[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]


Out[18]:
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

and it’s equivalent to:


In [19]:
combs = []
for x in [1,2,3]:
    for y in [3,1,4]:
        if x != y:
            combs.append((x, y))

combs


Out[19]:
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

Note how the order of the for and if statements is the same in both these snippets.

If the expression is a tuple (e.g. the (x, y) in the previous example), it must be parenthesized.

Examples


In [20]:
vec = [-4, -2, 0, 2, 4]
# create a new list with the values doubled
[x*2 for x in vec]


Out[20]:
[-8, -4, 0, 4, 8]

In [21]:
# filter the list to exclude negative numbers
[x for x in vec if x >= 0]


Out[21]:
[0, 2, 4]

In [22]:
# apply a function to all the elements
[abs(x) for x in vec]


Out[22]:
[4, 2, 0, 2, 4]

In [23]:
# call a method on each element
freshfruit = ['  banana', '  loganberry ', 'passion fruit  ']
[weapon.strip() for weapon in freshfruit]


Out[23]:
['banana', 'loganberry', 'passion fruit']

In [24]:
# create a list of 2-tuples like (number, square)
[(x, x**2) for x in range(6)]


Out[24]:
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]

In [25]:
# the tuple must be parenthesized, otherwise an error is raised
# [x, x**2 for x in range(6)]

In [26]:
# flatten a list using a listcomp with two 'for'
vec = [[1,2,3], [4,5,6], [7,8,9]]
[num for elem in vec for num in elem]


Out[26]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

List comprehensions can contain complex expressions and nested functions:


In [27]:
from math import pi
[str(round(pi, i)) for i in range(1, 6)]


Out[27]:
['3.1', '3.14', '3.142', '3.1416', '3.14159']

Nested List Comprehensions

The initial expression in a list comprehension can be any arbitrary expression, including another list comprehension.

Consider the following example of a 3x4 matrix implemented as a list of 3 lists of length 4:


In [28]:
matrix = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
]
matrix


Out[28]:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

The following list comprehension will transpose rows and columns:


In [29]:
[[row[i] for row in matrix] for i in range(4)]


Out[29]:
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

As we saw in the previous section, the nested listcomp is evaluated in the context of the for that follows it, so this example is equivalent to:


In [30]:
transposed = []
for i in range(4):
    transposed.append([row[i] for row in matrix])

transposed


Out[30]:
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

which, in turn, is the same as:


In [31]:
transposed = []
for i in range(4):
    # the following 3 lines implement the nested listcomp
    transposed_row = []
    for row in matrix:
        transposed_row.append(row[i])
    transposed.append(transposed_row)

transposed


Out[31]:
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

zip()

In the real world, you should prefer built-in functions to complex flow statements.

The zip() function would do a great job for this use case:


In [32]:
zip(*matrix)


Out[32]:
<zip at 0x10ff0fd08>

See Unpacking Argument Lists for details on the asterisk in this line.

The del statement

There is a way to remove an item from a list given its index instead of its value: the del statement.

This differs from the pop() method which returns a value.

The del statement can also be used to remove slices from a list or clear the entire list (which we did earlier by assignment of an empty list to the slice).

For example:


In [33]:
a = [-1, 1, 66.25, 333, 333, 1234.5]
del a[0]
a


Out[33]:
[1, 66.25, 333, 333, 1234.5]

In [34]:
del a[2:4]
a


Out[34]:
[1, 66.25, 1234.5]

In [35]:
del a[:]
a


Out[35]:
[]

del can also be used to delete entire variables:


In [36]:
del a

Referencing the name a hereafter is an error (at least until another value is assigned to it). We’ll find other uses for del later.

Tuples and Sequences

We saw that lists and strings have many common properties, such as indexing and slicing operations.

They are two examples of sequence data types (see Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange).

Since Python is an evolving language, other sequence data types may be added.

There is also another standard sequence data type: the tuple.

A tuple consists of a number of values separated by commas, for instance:


In [37]:
t = 12345, 54321, 'hello!'
t[0]


Out[37]:
12345

In [38]:
t


Out[38]:
(12345, 54321, 'hello!')

In [39]:
# Tuples may be nested:
u = t, (1, 2, 3, 4, 5)
u


Out[39]:
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))

In [40]:
# Tuples are immutable:
# t[0] = 88888

In [41]:
# but they can contain mutable objects:
v = ([1, 2, 3], [3, 2, 1])
v


Out[41]:
([1, 2, 3], [3, 2, 1])

Parentheses

As you see, on output tuples are always enclosed in parentheses, so that nested tuples are interpreted correctly.

They may be input with or without surrounding parentheses, although often parentheses are necessary anyway (if the tuple is part of a larger expression).

It is not possible to assign to the individual items of a tuple, however it is possible to create tuples which contain mutable objects, such as lists.

Lists vs Tuples

Though tuples may seem similar to lists, they are often used in different situations and for different purposes.

Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples).

Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.

Tuple Creation

A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these.

Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses).

Ugly, but effective. For example:


In [42]:
empty = ()
singleton = 'hello',    # <-- note trailing comma
len(empty)


Out[42]:
0

In [43]:
len(singleton)


Out[43]:
1

In [44]:
singleton


Out[44]:
('hello',)

The statement t = 12345, 54321, 'hello!' is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple. The reverse operation is also possible:


In [45]:
x, y, z = t
print(x)
print(y)
print(z)


12345
54321
hello!

This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side.

Sequence unpacking requires the list of variables on the left to have the same number of elements as the length of the sequence.

Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.

Sets

Python also includes a data type for sets.

A set is an unordered collection with no duplicate elements.

Basic uses include membership testing and eliminating duplicate entries.

Set objects also support mathematical operations like

  • union
  • intersection
  • difference
  • symmetric difference.

Curly braces or the set() function can be used to create sets.

Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.

Here is a brief demonstration:


In [46]:
basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
fruit = set(basket)               # create a set without duplicates
fruit


Out[46]:
{'apple', 'banana', 'orange', 'pear'}

In [47]:
'orange' in fruit                 # fast membership testing


Out[47]:
True

In [48]:
'crabgrass' in fruit


Out[48]:
False

In [49]:
# Demonstrate set operations on unique letters from two words
a = set('abracadabra')
b = set('alacazam')
a                                  # unique letters in a


Out[49]:
{'a', 'b', 'c', 'd', 'r'}

In [50]:
a - b                              # letters in a but not in b


Out[50]:
{'b', 'd', 'r'}

In [51]:
a | b                              # letters in either a or b


Out[51]:
{'a', 'b', 'c', 'd', 'l', 'm', 'r', 'z'}

In [52]:
a & b                              # letters in both a and b


Out[52]:
{'a', 'c'}

In [53]:
a ^ b                              # letters in a or b but not both


Out[53]:
{'b', 'd', 'l', 'm', 'r', 'z'}

Similarly to list comprehensions, set comprehensions are also supported:


In [54]:
a = {x for x in 'abracadabra' if x not in 'abc'}
a


Out[54]:
{'d', 'r'}

Dictionaries

Another useful data type built into Python is the dictionary (see Mapping Types — dict).

Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”.

Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys.

Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key.

You can’t use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like append() and extend().

It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary; this is also the way dictionaries are written on output.

The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:value pair with del. If you store using a key that is already in use, the old value associated with that key is forgotten. It is an error to extract a value using a non-existent key.

The keys() method of a dictionary object returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just apply the sorted() function to it). To check whether a single key is in the dictionary, use the in keyword.

Here is a small example using a dictionary:


In [55]:
tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127
tel


Out[55]:
{'guido': 4127, 'jack': 4098, 'sape': 4139}

In [56]:
tel['jack']


Out[56]:
4098

In [57]:
del tel['sape']
tel['irv'] = 4127
tel


Out[57]:
{'guido': 4127, 'irv': 4127, 'jack': 4098}

In [58]:
tel.keys()


Out[58]:
dict_keys(['jack', 'guido', 'irv'])

In [59]:
'guido' in tel


Out[59]:
True

The dict() constructor builds dictionaries directly from sequences of key-value pairs:


In [60]:
dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])


Out[60]:
{'guido': 4127, 'jack': 4098, 'sape': 4139}

In addition, dict comprehensions can be used to create dictionaries from arbitrary key and value expressions:


In [61]:
{x: x**2 for x in (2, 4, 6)}


Out[61]:
{2: 4, 4: 16, 6: 36}

When the keys are simple strings, it is sometimes easier to specify pairs using keyword arguments:


In [62]:
dict(sape=4139, guido=4127, jack=4098)


Out[62]:
{'guido': 4127, 'jack': 4098, 'sape': 4139}

Looping Techniques

When looping through a sequence, the position index and corresponding value can be retrieved at the same time using the enumerate() function.


In [63]:
for i, v in enumerate(['tic', 'tac', 'toe']):
    print(i, v)


0 tic
1 tac
2 toe

zip()

To loop over two or more sequences at the same time, the entries can be paired with the zip() function.


In [64]:
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.

reversed()

To loop over a sequence in reverse, first specify the sequence in a forward direction and then call the reversed() function.


In [66]:
for i in reversed(range(1,10,2)):
    print(i)


9
7
5
3
1

sorted()

To loop over a sequence in sorted order, use the sorted() function which returns a new sorted list while leaving the source unaltered.


In [67]:
basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
for f in sorted(set(basket)):
    print(f)


apple
banana
orange
pear

items()

When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the items() method.


In [69]:
knights = {'gallahad': 'the pure', 'robin': 'the brave'}
for k, v in knights.items():
    print(k, v)


gallahad the pure
robin the brave

It is sometimes tempting to change a list while you are looping over it; however, it is often simpler and safer to create a new list instead.


In [70]:
import math
raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
filtered_data = []
for value in raw_data:
    if not math.isnan(value):
        filtered_data.append(value)

filtered_data


Out[70]:
[56.2, 51.7, 55.3, 52.5, 47.8]