< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039199167010047123209178152124239215162145080038235019135135235

Data Structures

Python implements a variety of data structures, such as lists, tuples, ranges, and dictionaries (also known as hash tables).

Lists

Lists are mutable sequences of objects indexed by natural numbers that can be changed after they are created.

Lists are very flexible and easy to create. They are always enclosed in brackets:

						
>>> lst = [1,2,3,4]  # this is simple list

					

A list can have elements of different data types:

						
>>> lst = [1, "ni!", 2]

					

Lists can also include other lists:

						
>>> lst = [1, "ni!", [1,2,"Albatross!!"]]

					

A list uses the same operators that strings use. For example, you need to use slice notation to grab a range of elements from a list.

						
>>> lst = [1, "ni!", [1, 2, 3, 4, "Albatross!!", 3]]
>>> lst[1]
"ni!"

					

To grab elements from lists that are located inside other lists, you need to use a pair of brackets to represent each list. Check out the next couple of examples.

						
>>> lst = [1, "ni!", [1, 2, 3, 4, "Albatross!!", 3]]
>>> lst[2][4]
"Albatross!!"
>>> lst[2][4][5]
"r"

					

Let's see some examples of operations that can be applied to a list.

Identifying an Entry
							
>>> lst = ["p", "a", "r", "r", "o", "t"]
>>> lst.index("o")
4

						
Assigning Values to a List
							
>>> lst = ["p", "a", "r", "r", "o", "t"]
>>> lst[1] = "aaaaaaaaaaaaa"
>>> lst
["p", "aaaaaaaaaaaaa", "r", "r", "o", "t"]

						
Assigning Values to a Slice
							
>>> lst = ["p", "a", "r", "r", "o", "t"]
>>> lst[1:4] = ["aaaaaaaaaaaaa", "rrr", "rrrr"]
>>> lst
["p", "aaaaaaaaaaaaa", "rrr", "rrrr", "o", "t"]

						
Inserting Values

The following example starts inserting values at index number 6.

							
>>> lst = ["p", "a", "r", "r", "o", "t"]
>>> lst[6:] = [" ", "s", "k", "e", "t", "c", "h"]
['p', 'a', 'r', 'r', 'o', 't', '', 's', 'k', 'e', 't', 'c', 'h']

						

If the list was longer than 6 elements, the statement would overwrite a portion of the list. Note that you can also insert a value in this list with

							
>>> lst.insert(6, val)

						
Deleting a Value
							
>>> lst = ["p", "a", "r", "r", "o", "t"]
>>> del lst[-1]
>>> lst
["p", "a", "r", "r", "o"]
>>> del lst[0:2]
["r", "r", "o"]

						

The following example converts objects to their string representation:

							
>>> lst = [10,20,30,"inquisition","lumberjack"]
>>> text = ""
>>> for element in lst:
…     text = text + `element`
…     # enables the concatenation of any object
…     print text
…
10
1020
102030
102030'inquisition'
102030'inquisition''lumberjack'
					
						
List Comprehension

Starting with release 2.0, there is a new notation to create lists whose elements are computed from another list (or lists). The method is called List Comprehension, and it adopts the following format:

							
[ expression for expression1 in sequence1
              [for expression2 in sequence2]
              [ for expressionN in sequenceN]
              [if condition] ]

						

All for…in clauses are evaluated and iterated from left to right. That means that the resulting list is a cartesian product of the given sequences. For example, if you have three lists of length 5, the output list has 125 elements. The if clause is optional, but when present, it can limit the number of pairs that will become part of the resulting list by adding pairs to the resulting list only when the result condition of the if statement evaluates to true. Check the following example:

							
letters = 'py'
numbers = (1.52, 1.6, 2.0)
>>> [ (l,n) for l in letters for n in numbers]
[('p', 1.52), ('p', 1.6), ('p', 2.0), ('y', 1.52), ('y', 1.6),
('y', 2.0)]

						

This new concept is more efficient than a for loop with an if statement along with a list.append() function call.

Built-In Methods

To list all the built-in methods of a list, go to the interpreter and type dir([]).

Let's practice the methods that you have found, and see what happens to our list lst.

						
>>> lst = [0, 1, 2]
>>> lst.append(5)         # appends the element 5 to the list
>>> lst
[0, 1, 2, 5]
>>> lst.append((5, 6))    # appends the tuple (5, 6)
>>> lst
[0, 1, 2, 5, (5, 6)]
>>> lst.pop()             # removes the last element of the list
(5, 6)
>>> lst
[0, 1, 2, 5]
>>> lst.insert(2,7)       # inserts the element 7 at index number 2
>>> lst
[0, 1, 7, 2, 5]
>>> lst.pop(2)            # removes the element at index number 2
7
>>> lst
[0, 1, 2, 5]
>>> lst.reverse()         # reverse the list order
>>> lst
[5, 2, 1, 0]
>>> lst.sort()            # sorts the list elements
>>> lst
[0, 1, 2, 5]
>>> lst.extend([3, 4, 5]) # adds this list to our original list
>>> lst
[0, 1, 2, 5, 3, 4, 5]
>>> lst.count(5) # counts the number of elements number 5 that exist.
2
>>> lst.index(3) # returns the associated index of element 3.
4
>>> lst.remove(2) # removes the element number 2 (not the index!!!)
>>> lst
[0, 1, 5, 3, 4, 5] 
				
					

Note that up to release 1.5.2, whenever you used lst.append (1,2), a tuple (1,2) would be appended to the list lst. Now, with release 2.0, when you do that, you get an TypeError exception followed by a message like " append requires exactly 1 argument; 2 given ". Don't panic! To fix that, you just need to add an extra pair of parenthesis, like this: lst.append ((1,2)).

Ranges

A range is an actual list of integers. The built-in function range() provides this data structure.

						
>>> r = range(2,5)
>>> print r
[2,3,4]

					

When the first argument is left out, it is assumed to be zero.

						
>>> r = range(3)
>>> print r
[0,1,2]

					

When you provide a third argument to the range() function, you specify the interval that you want to exist between the list elements.

						
>>> r = range(2,10,2)
>>> print r
[2, 4, 6, 8]

					

Let's see an example of stepping backward:

						
>>> r = range(5,1,-1)
>>> print r
[5, 4, 3, 2]

					

The xrange() function computes the values only when they are accessed. This function returns an XrangeType object, instead of storing a large list of numbers in a variable.

						
>>> for n in xrange(10):
…          print n,
…
0, 1, 2, 3, 4, 5, 6, 7, 8, 9

					

The previous example also works with the range() function, although it will store the whole list in memory.

It is possible to assign a reference to the return value of the xrange() function to a variable, as you will see next. Note that we are not storing the values, only a reference to the function.

						
>>> lst = xrange(10)
>>> lst
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

					

However, you can convert this reference later into a real list by using the tolist() method.

						
>>> lst.tolist()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
				
					

Tuples

A tuple is a sequence of immutable Python objects.

The general syntax of a tuple is as follows:

						
variable = (element1, element2, …)

					

It looks like a list without the brackets. Note in the following examples that parentheses are optional.

						
>>> t = (1,)
>>> print t
(1,)
>>> t = 1,
>>> print t
(1,)
>>> t = ()        # this is an empty tuple.
>>> print t
()
>>> t = (1,2,3)
>>> print t
(1,2,3)
>>> t = 1,2,3
>>> print t
(1,2,3)
				
					

Note that in the previous example, it is necessary to use the comma when defining a length-1 tuple. Otherwise, the variable being created wouldn't be defined as of type tuple. Instead, the interpreter would think that you wanted to assign a numeric value to the variable.

A tuple really looks like a list. The difference between tuples and lists is that tuples are immutable.

You can bypass this rule if you bind a new structure to the old tuple variable.

						
>>> t = 10,15,20
>>> t = t[0],t[2]
>>> t
(10,20)

					
Other Interesting Facts About Tuples
  • They support indexing.

    									
    >>> t = 10,20,30,40
    >>> print t[1]
    20
    
    								
  • You will see, later in this chapter, that you need to use tuples whenever you need to return more than one value from a function.

    									
    >>> Def tuplefunction():
    …            return 10, 20, 30
    …
    >>> x, y, z = tuplefunction()
    >>> print x, y, z
    10 20 30
    							
    								

Dictionaries (hash tables)

Dictionaries illustrate the only mapping type of Python. They represent finite sets of objects indexed by nearly arbitrary values. I say nearly because dictionary keys cannot be variables of mutable type, which are compared by value rather than by object identity.

Python dictionaries are also known as associative arrays or hash tables. The general syntax of a dictionary is as follows:

						
variable = {"key1":"value1", "key2":"value2", …}

					

Dictionaries are always enclosed in braces. They associate key elements with value elements—keys and values are displayed separated by a colon.

The values of a dictionary can be of any type, but the keys must be of an immutable data type (such as strings, numbers, or tuples). Dictionary keys have no natural order and are always listed in arbitrary order because it uses a hash technique to implement a fast lookup.

Let's focus now on the operations that we can implement with dictionaries. First, let's create a simple dictionary.

						
>>> dic = {"bird":"parrot", "fish":"tuna", "dino":"t-rex"}

					

Now, let's apply some operations to it:

						
>>> dic["fish"]      # value lookup
"tuna"
>>> dic["animal"]    # raises a KeyError exception
Traceback (innermost last):
  File "<stdin>", line 1, in ?
KeyError: animal
>>> del dic["fish"]  # deletes the key fish
>>> print dic
{'bird': 'parrot', 'dino': 't-rex'}
>>> dic["dino"] = "brontosaur"  # updates an entry
>>> dic["parrot age"] = 58	        # adds an entry
>>> dic
{"bird": "parrot", "dino": "brontosaur", "parrot age": 58}
>>> len(dic)          # provides the number of keys
3

					
Built-In Methods

The following sequence of commands shows the built-in methods that are implemented for dictionaries.

							
>>> dic = {"a":1, "b":2, "c":3}
>>> dic.keys()   # creates a list of keys. Very used in for statements.
["a","b","c"]
>>> dic.values()     # creates a list of values
["1","2","3"]
>>> dic.items()      # creates a tuple with the dictionary elements
[("a","1"),("b","2"),("c","3")]
>>> dic.has_key("a") # returns 1 if key exists. Otherwise it returns 0.
1

# dic.get(value, default)
# If key exists, returns its value. Otherwise it returns the second arg.
>>> dic.get("b", None)
2

# dic.update(dictionary)
# adds the dictionary in the argument to the original dictionary.
>>> dic.update({"d":4})

>>> newdic = dic.copy()        # creates a copy of the dictionary
>>> keys = dic.keys()
>>> keys.sort()                 # sorts the dictionary keys
>>> dic.clear()          # removes all the items from the dictionary.
					
						

Python 2.0 contains a brand-new method for dictionaries, which is called setdefault(). This method returns the value for the given key (exactly as the get() method would do). However, if the given key is not found, it returns the given default value, and at the same time, it initializes the given key with the default value, as demonstrated in the following code.

							
if dict.has_key( key ):
    return dict[key]
else:
    dict[key] = ["default value"]
    return dict[key]

						

is the same of saying

							
return dict.setdefault(key, "default value")

						


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

()_(parenthesis)
      1st append
1st.append
      () (parenthesis)
applying
      operations to dictionaries
arguments
      range() function
backward stepping
converting
      references into lists
creating
      dictionaries
data structures 2nd 3rd 4th 5th 6th
dictionaries
      methods
functions
      returning values from, tuples
      xrange()
indexing
      support, tuples
List Comprehension 2nd
lists 2nd
      converting references into
      vs. tuples
medhots
      tolist()
methods
      for dictionaries
      setdefault
operations
      applying to dictionaries
operators
      lists
parenthesis (_)
      1st append
references
      converting into lists
returning
      values from functions, tuples
setdefault() method
stepping backward
structures
      data 2nd 3rd 4th 5th 6th
support
      indexing, tuples
syntax
      tuples
tolist() method
tuples
values
      returning from functions, tuples
xrange() function

© 2002, O'Reilly & Associates, Inc.