36. Our last sequence

Lists and strings can both be sliced, using the [::] notation. Individual items can be found through their index, that is with the notation [index]. Since they have these properties, lists and strings are known as sequences data types. Python has other similar data types which are sequences; one of the most common ones, and the last one we will see, is known as a tuple.


A comma is a powerful thing

Remember when we tried to use a comma while writing a large number? Here's another example:

>>> 1,000
(1, 0)

Python has taken the two values separated by a comma to be the elements of a tuple. When it writes out a tuple, Python puts parentheses around it to indicate that it is a single object.

Note: if you get an error message while attempting to do an example similar to the one we showed (but with different numbers) or get a result you can not explain given what you see here, ignore it for now. We'll come back to it at the end of this lesson.

Here's perhaps a more interesting example.

>>> a = 1, 2, 3, 4, "enough!"
>>> print a
(1, 2, 3, 4, 'enough!')

>>> a[0]
1

>>> a[1:4]
(2, 3, 4)

>>> a[-1]
'enough!'

We can also put tuples inside tuples. Here are two examples.

>>> b = 1, 2, 3

>>> c = 4, 5, b
>>> print c
(4, 5, (1, 2, 3))

>>> d = 1, 2, (3, 4, 5)
>>> print d
(1, 2, (3, 4, 5))

Other than using parentheses () instead of square brackets [], it would appear that tuples and lists are the same. However, they differ in a very important way:

>>> my_tuple = 1, 2, 3
>>> my_list = [1, 2, 3]

>>> print my_tuple
(1, 2, 3)
>>> print my_list
[1, 2, 3]

>>> my_list[1] = "new"
>>> print my_list
[1, 'new', 3]

>>> my_tuple[1] = "new"
Traceback (most recent call last):
  File "<input>", line 1, in ?
TypeError: object does not support item assignment

Whereas we can change a list, by changing one of its element (or by adding to it), we can not change a tuple: it is said to be immutable. In addition to numbers, an other immutable data type we have seen is the string:

>>> my_string = "abcd"
>>> my_string[2]
'c'
>>> my_string[2] = "e"
Traceback (most recent call last):
  File "<input>", line 1, in ?
TypeError: object does not support item assignment

Since tuples are immutable, it means that they can be used as keys in a dict!


dict, lists and tuples

We have seen before that Python keeps a list with the elements in the same order as we create them, but that it prints out a dict in what appears to be a random order. We can see this happening in Reeborg's world.

Click on the "Show/Hide world file" button Show/Hide world file to display the world file on the right hand side. Now, click on the edit wall button: edit walls button.

Create a few walls, and watch what happens in the world file. Each wall created is a list element whose value is a tuple with the world coordinate. Each wall that is created is appended at the end of the list. When you delete a wall, by clicking on an existing wall, it is removed from the list at that location. If you immediately click again to recreate that wall, it appears in the list at the end, being appended again.

Contrast this with beepers. You can right-click on any street corner to specify the number of beepers to put there. If you change the value of the beepers at a given corner, they get changed in-place. If you add beepers at a corner where there were none before, the new beepers are, in general, not appended at the end. (You may need to have more than a few corners with beepers to see this.) Make sure you have many corners with beepers. Select a corner with beepers and set the value to zero, watching the world file. Like for the walls, the entry disappears. Now, immediately put some beepers back on that same corner. The new entry appears at the original place, and not at the end like it was the case for the list. This is because the order in which items appear in a dict is based on a mathematical function ("hash") of the dict "key" (the tuple with the world coordinate in this case) which Python knows how to use efficiently, but which makes it appear as random to us.

previousAn other kind of definition - home - Fixing up Reeborg next