Archive:Think Python/Tuples: Difference between revisions
wikademia>Whiteknight m Think Python: Automatically uploading HTML source of this book from http://www.greenteapress.com/thinkpython/html/. Will convert to wikitext in a separate step |
wikademia>Whiteknight m Partial (mostly) conversion from HTML to Wikitext |
||
| Line 1: | Line 1: | ||
{{Think Python/Page}} | |||
== Chapter 12  Tuples == | |||
=== 12.1  Tuples are immutable === | |||
A tuple is a sequence of values. The values can be any type, and | |||
they are indexed by integers, so in that respect tuples are a lot | they are indexed by integers, so in that respect tuples are a lot | ||
like lists. The important difference is that tuples are immutable. | like lists. The important difference is that tuples are immutable. | ||
parentheses: | |||
comma: | Syntactically, a tuple is a comma-separated list of values: | ||
<PRE CLASS="verbatim">>>> t = 'a', 'b', 'c', 'd', 'e' | |||
</PRE> | |||
Although it is not necessary, it is common to enclose tuples in | |||
parentheses: | |||
<PRE CLASS="verbatim">>>> t = ('a', 'b', 'c', 'd', 'e') | |||
</PRE> | |||
To create a tuple with a single element, you have to include the final | |||
comma: | |||
<PRE CLASS="verbatim">>>> t1 = ('a',) | |||
>>> type(t1) | >>> type(t1) | ||
<type 'tuple'> | <type 'tuple'> | ||
</PRE> | |||
parentheses: | Without the comma, Python treats <CODE>('a')</CODE> as a string in | ||
parentheses: | |||
<PRE CLASS="verbatim">>>> t2 = ('a') | |||
>>> type(t2) | >>> type(t2) | ||
<type 'str'> | <type 'str'> | ||
</PRE> | |||
With no argument, it creates an empty tuple: | Another way to create a tuple is the built-in function <TT>tuple</TT>. | ||
With no argument, it creates an empty tuple: | |||
<PRE CLASS="verbatim">>>> t = tuple() | |||
>>> print t | >>> print t | ||
() | () | ||
</PRE> | |||
is a tuple with the elements of the sequence: | If the argument is a sequence (string, list or tuple), the result | ||
is a tuple with the elements of the sequence: | |||
<PRE CLASS="verbatim">>>> t = tuple('lupins') | |||
>>> print t | >>> print t | ||
('l', 'u', 'p', 'i', 'n', 's') | ('l', 'u', 'p', 'i', 'n', 's') | ||
</PRE> | |||
avoid using it as a variable name. | Because <TT>tuple</TT> is the name of a built-in function, you should | ||
indexes an element: | avoid using it as a variable name. | ||
Most list operators also work on tuples. The bracket operator | |||
indexes an element: | |||
<PRE CLASS="verbatim">>>> t = ('a', 'b', 'c', 'd', 'e') | |||
>>> print t[0] | >>> print t[0] | ||
'a' | 'a' | ||
</PRE> | |||
And the slice operator selects a range of elements. | |||
<PRE CLASS="verbatim">>>> print t[1:3] | |||
('b', 'c') | ('b', 'c') | ||
</PRE> | |||
an error: | But if you try to modify one of the elements of the tuple, you get | ||
an error: | |||
<PRE CLASS="verbatim">>>> t[0] = 'A' | |||
TypeError: object doesn't support item assignment | TypeError: object doesn't support item assignment | ||
</PRE> | |||
one tuple with another: | You can’t modify the elements of a tuple, but you can replace | ||
one tuple with another: | |||
<PRE CLASS="verbatim">>>> t = ('A',) + t[1:] | |||
>>> print t | >>> print t | ||
('A', 'b', 'c', 'd', 'e') | ('A', 'b', 'c', 'd', 'e') | ||
</PRE>=== 12.2  Tuple assignment === | |||
It is often useful to swap the values of two variables. | |||
With conventional assignments, you have to use a temporary | With conventional assignments, you have to use a temporary | ||
variable. For example, to swap | variable. For example, to swap <TT>a</TT> and <TT>b</TT>: | ||
<PRE CLASS="verbatim">>>> temp = a | |||
>>> a = b | >>> a = b | ||
>>> b = temp | >>> b = temp | ||
</PRE> | |||
This solution is cumbersome; '''tuple assignment''' is more elegant: | |||
<PRE CLASS="verbatim">>>> a, b = b, a | |||
</PRE> | |||
The left side is a tuple of variables; the right side is a tuple of | |||
expressions. Each value is assigned to its respective variable. | expressions. Each value is assigned to its respective variable. | ||
All the expressions on the right side are evaluated before any | All the expressions on the right side are evaluated before any | ||
of the assignments. | of the assignments. | ||
values on the right have to be the same: | |||
The number of variables on the left and the number of | |||
values on the right have to be the same: | |||
<PRE CLASS="verbatim">>>> a, b = 1, 2, 3 | |||
ValueError: too many values to unpack | ValueError: too many values to unpack | ||
</PRE> | |||
More generally, the right side can be any kind of sequence | |||
(string, list or tuple). For example, to split an email address | (string, list or tuple). For example, to split an email address | ||
into a user name and a domain, you could write: | into a user name and a domain, you could write: | ||
<PRE CLASS="verbatim">>>> addr = 'monty@python.org' | |||
>>> uname, domain = addr.split('@') | >>> uname, domain = addr.split('@') | ||
</PRE> | |||
the first element is assigned to | The return value from <TT>split</TT> is a list with two elements; | ||
the first element is assigned to <TT>uname</TT>, the second to | |||
<TT>domain</TT>. | |||
<PRE CLASS="verbatim">>>> print uname | |||
monty | monty | ||
>>> print domain | >>> print domain | ||
python.org | python.org | ||
</PRE>=== 12.3  Tuples as return values === | |||
Strictly speaking, a function can only return one value, but | |||
if the value is a tuple, the effect is the same as returning | if the value is a tuple, the effect is the same as returning | ||
multiple values. For example, if you want to divide two integers | multiple values. For example, if you want to divide two integers | ||
and compute the quotient and remainder, it is inefficient to | and compute the quotient and remainder, it is inefficient to | ||
compute | compute <TT>x/y</TT> and then <TT>x%y</TT>. It is better to compute | ||
them both at the same time. | them both at the same time. | ||
The built-in function <TT>divmod</TT> takes two arguments and | |||
returns a tuple of two values, the quotient and remainder. | returns a tuple of two values, the quotient and remainder. | ||
You can store the result as a tuple: | You can store the result as a tuple: | ||
<PRE CLASS="verbatim">>>> t = divmod(7, 3) | |||
>>> print t | >>> print t | ||
(2, 1) | (2, 1) | ||
</PRE> | |||
Or use tuple assignment to store the elements separately: | |||
<PRE CLASS="verbatim">>>> quot, rem = divmod(7, 3) | |||
>>> print quot | >>> print quot | ||
2 | 2 | ||
>>> print rem | >>> print rem | ||
1 | 1 | ||
</PRE> | |||
Here is an example of a function that returns a tuple: | |||
<PRE CLASS="verbatim">def min_max(t): | |||
return min(t), max(t) | return min(t), max(t) | ||
</PRE> | |||
the largest and smallest elements of a sequence. | <TT>max</TT> and <TT>min</TT> are built-in functions that find | ||
computes both and returns a tuple of two values. | the largest and smallest elements of a sequence. <CODE>min_max</CODE> | ||
computes both and returns a tuple of two values. | |||
=== 12.4  Variable-length argument tuples === | |||
name that begins with | |||
a tuple. For example, | |||
takes any number of arguments and prints them: | |||
Functions can take a variable number of arguments. A parameter | |||
name that begins with <TT>*</TT> '''gathers''' arguments into | |||
a tuple. For example, <TT>printall</TT> | |||
takes any number of arguments and prints them: | |||
<PRE CLASS="verbatim">def printall(*args): | |||
print args | print args | ||
</PRE> | |||
conventional. Here’s how the function works: | The gather parameter can have any name you like, but <TT>args</TT> is | ||
conventional. Here’s how the function works: | |||
<PRE CLASS="verbatim">>>> printall(1, 2.0, '3') | |||
(1, 2.0, '3') | (1, 2.0, '3') | ||
</PRE> | |||
arguments: | You can combine the gather operator with required and positional | ||
arguments: | |||
<PRE CLASS="verbatim">def pointless(required, optional=0, *args): | |||
print required, optional, args | print required, optional, args | ||
</PRE> | |||
make sure you understand what it does. | Run this function with 1, 2, 3 and 4 or more arguments and | ||
make sure you understand what it does. | |||
The complement of gather is '''scatter'''. If you have a | |||
sequence of values and you want to pass it to a function | sequence of values and you want to pass it to a function | ||
as multiple arguments, you can use the | as multiple arguments, you can use the <TT>*</TT> operator. | ||
For example, | For example, <TT>divmod</TT> takes exactly two arguments; it | ||
doesn’t work with a tuple: | doesn’t work with a tuple: | ||
<PRE CLASS="verbatim">>>> t = (7, 3) | |||
>>> divmod(t) | >>> divmod(t) | ||
TypeError: divmod expected 2 arguments, got 1 | TypeError: divmod expected 2 arguments, got 1 | ||
</PRE> | |||
But if you scatter the tuple, it works: | |||
<PRE CLASS="verbatim">>>> divmod(*t) | |||
(2, 1) | (2, 1) | ||
</PRE><DIV CLASS="theorem">'''Exercise 1'''  '' | |||
Many of the built-in functions use | Many of the built-in functions use | ||
variable-length argument tuples. For example, | variable-length argument tuples. For example, ''''<TT>max</TT>'''' | ||
and | and ''''<TT>min</TT>'''' can take any number of arguments:'' | ||
'' | |||
'''' | |||
'''' | |||
'' | |||
<PRE CLASS="verbatim">''>>> max(1,2,3) | |||
3 | 3 | ||
''</PRE> | |||
''But ''''<TT>sum</TT>'''' does not.'' | |||
'' | |||
'' | |||
<PRE CLASS="verbatim">''>>> sum(1,2,3) | |||
TypeError: sum expected at most 2 arguments, got 3 | TypeError: sum expected at most 2 arguments, got 3 | ||
''</PRE> | |||
of arguments and returns their sum. | ''Write a function called ''''<TT>sumall</TT>'''' that takes any number | ||
of arguments and returns their sum.'' | |||
“zips” them into a list | </DIV>=== 12.5  Lists and tuples === | ||
sequence. | |||
<TT>zip</TT> is a built-in function that takes two or more sequences and | |||
“zips” them into a list<SUP>1</SUP> of tuples where each tuple contains one element from each | |||
sequence. | |||
This example zips a string and a list: | |||
<PRE CLASS="verbatim">>>> s = 'abc' | |||
>>> t = [0, 1, 2] | >>> t = [0, 1, 2] | ||
>>> zip(s, t) | >>> zip(s, t) | ||
[('a', 0), ('b', 1), ('c', 2)] | [('a', 0), ('b', 1), ('c', 2)] | ||
</PRE> | |||
The result is a list of tuples where each tuple contains | |||
a character from the string and the corresponding element from | a character from the string and the corresponding element from | ||
the list. | the list. | ||
length of the shorter one. | |||
If the sequences are not the same length, the result has the | |||
length of the shorter one. | |||
<PRE CLASS="verbatim">>>> zip('Anne', 'Elk') | |||
[('A', 'E'), ('n', 'l'), ('n', 'k')] | [('A', 'E'), ('n', 'l'), ('n', 'k')] | ||
</PRE> | |||
tuples: | You can use tuple assignment in a <TT>for</TT> loop to traverse a list of | ||
tuples: | |||
<PRE CLASS="verbatim">t = [('a', 0), ('b', 1), ('c', 2)] | |||
for letter, number in t: | for letter, number in t: | ||
print number, letter | print number, letter | ||
</PRE> | |||
the list and assigns the elements to | Each time through the loop, Python selects the next tuple in | ||
the list and assigns the elements to <TT>letter</TT> and | |||
<TT>number</TT>. The output of this loop is: | |||
<PRE CLASS="verbatim">0 a | |||
1 b | 1 b | ||
2 c | 2 c | ||
</PRE> | |||
If you combine <TT>zip</TT>, <TT>for</TT> and tuple assignment, you get a | |||
useful idiom for traversing two (or more) sequences at the same | useful idiom for traversing two (or more) sequences at the same | ||
time. For example, | time. For example, <CODE>has_match</CODE> takes two sequences, <TT>t1</TT> and | ||
<TT>t2</TT>, and returns <TT>True</TT> if there is an index <TT>i</TT> | |||
such that | such that <TT>t1[i] == t2[i]</TT>: | ||
<PRE CLASS="verbatim">def has_match(t1, t2): | |||
for x, y in zip(t1, t2): | for x, y in zip(t1, t2): | ||
if x == y: | if x == y: | ||
return True | return True | ||
return False | return False | ||
</PRE> | |||
indices, you can use the built-in function | If you need to traverse the elements of a sequence and their | ||
indices, you can use the built-in function <TT>enumerate</TT>: | |||
<PRE CLASS="verbatim">for index, element in enumerate('abc'): | |||
print index, element | print index, element | ||
</PRE> | |||
The output of this loop is: | |||
<PRE CLASS="verbatim">0 a | |||
1 b | 1 b | ||
2 c | 2 c | ||
</PRE> | |||
Again. | |||
=== 12.6  Dictionaries and tuples === | |||
tuples, where each tuple is a key-value pair | |||
Dictionaries have a method called <TT>items</TT> that returns a list of | |||
tuples, where each tuple is a key-value pair<SUP>2</SUP>. | |||
<PRE CLASS="verbatim">>>> d = {'a':0, 'b':1, 'c':2} | |||
>>> t = d.items() | >>> t = d.items() | ||
>>> print t | >>> print t | ||
[('a', 0), ('c', 2), ('b', 1)] | [('a', 0), ('c', 2), ('b', 1)] | ||
</PRE> | |||
particular order. | As you should expect from a dictionary, the items are in no | ||
a new dictionary: | particular order. | ||
Conversely, you can use a list of tuples to initialize | |||
a new dictionary: | |||
<PRE CLASS="verbatim">>>> t = [('a', 0), ('c', 2), ('b', 1)] | |||
>>> d = dict(t) | >>> d = dict(t) | ||
>>> print d | >>> print d | ||
{'a': 0, 'c': 2, 'b': 1} | {'a': 0, 'c': 2, 'b': 1} | ||
</PRE> | |||
to create a dictionary: | Combining <TT>dict</TT> with <TT>zip</TT> yields a concise way | ||
to create a dictionary: | |||
<PRE CLASS="verbatim">>>> d = dict(zip('abc', range(3))) | |||
>>> print d | >>> print d | ||
{'a': 0, 'c': 2, 'b': 1} | {'a': 0, 'c': 2, 'b': 1} | ||
</PRE> | |||
and adds them, as key-value pairs, to an existing dictionary. | The dictionary method <TT>update</TT> also takes a list of tuples | ||
and adds them, as key-value pairs, to an existing dictionary. | |||
get the idiom for traversing the keys and values of a dictionary: | |||
Combining <TT>items</TT>, tuple assignment and <TT>for</TT>, you | |||
get the idiom for traversing the keys and values of a dictionary: | |||
<PRE CLASS="verbatim">for key, val in d.items(): | |||
print val, key | print val, key | ||
</PRE> | |||
The output of this loop is: | |||
<PRE CLASS="verbatim">0 a | |||
2 c | 2 c | ||
1 b | 1 b | ||
</PRE> | |||
Again. | |||
It is common to use tuples as keys in dictionaries (primarily because | |||
you can’t use lists). For example, a telephone directory might map | you can’t use lists). For example, a telephone directory might map | ||
from last-name, first-name pairs to telephone numbers. Assuming | from last-name, first-name pairs to telephone numbers. Assuming | ||
that we have defined | that we have defined <TT>last</TT>, <TT>first</TT> and <TT>number</TT>, we | ||
could write: | could write: | ||
<PRE CLASS="verbatim">directory[last,first] = number | |||
assignment to traverse this dictionary. | </PRE> | ||
The expression in brackets is a tuple. We could use tuple | |||
assignment to traverse this dictionary. | |||
<PRE CLASS="verbatim">for last, first in directory: | |||
print first, last, directory[last,first] | print first, last, directory[last,first] | ||
</PRE> | |||
assigns the elements of each tuple to | This loop traverses the keys in <TT>directory</TT>, which are tuples. It | ||
prints the name and corresponding telephone number. | assigns the elements of each tuple to <TT>last</TT> and <TT>first</TT>, then | ||
prints the name and corresponding telephone number. | |||
There are two ways to represent tuples in a state diagram. The more | |||
detailed version shows the indices and elements just as they appear in | detailed version shows the indices and elements just as they appear in | ||
a list. For example, the tuple | a list. For example, the tuple <CODE>('Cleese', 'John')</CODE> would appear: | ||
<DIV CLASS="center"><IMG SRC="book020.png"></DIV> | |||
But in a larger diagram you might want to leave out the | |||
details. For example, a diagram of the telephone directory might | details. For example, a diagram of the telephone directory might | ||
appear: | appear: | ||
shorthand. | <DIV CLASS="center"><IMG SRC="book021.png"></DIV> | ||
BBC, so please don’t call it. | Here the tuples are shown using Python syntax as a graphical | ||
shorthand. | |||
The telephone number in the diagram is the complaints line for the | |||
BBC, so please don’t call it. | |||
=== 12.7  Comparing tuples === | |||
The comparison operators work with tuples and other sequences; | |||
Python starts by comparing the first element from each | Python starts by comparing the first element from each | ||
sequence. If they are equal, it goes on to the next elements, | sequence. If they are equal, it goes on to the next elements, | ||
and so on, until it finds elements that differ. Subsequent | and so on, until it finds elements that differ. Subsequent | ||
elements are not considered (even if they are really big). | elements are not considered (even if they are really big). | ||
<PRE CLASS="verbatim">>>> (0, 1, 2) < (0, 3, 4) | |||
True | True | ||
>>> (0, 1, 2000000) < (0, 3, 4) | >>> (0, 1, 2000000) < (0, 3, 4) | ||
True | True | ||
</PRE> | |||
The <TT>sort</TT> function works the same way. It sorts | |||
primarily by first element, but in the case of a tie, it sorts | primarily by first element, but in the case of a tie, it sorts | ||
by second element, and so on. | by second element, and so on. | ||
with one or more sort keys preceding the elements from the sequence, | |||
This feature lends itself to a pattern called '''DSU''' for | |||
<DL CLASS="description"><DT CLASS="dt-description">'''Decorate'''</DT><DD CLASS="dd-description"> a sequence by building a list of tuples | |||
with one or more sort keys preceding the elements from the sequence,</DD><DT CLASS="dt-description">'''Sort'''</DT><DD CLASS="dd-description"> the list of tuples, and</DD><DT CLASS="dt-description">'''Undecorate'''</DT><DD CLASS="dd-description"> by extracting the sorted elements of the sequence.</DD></DL> | |||
sort them from longest to shortest: | |||
For example, suppose you have a list of words and you want to | |||
sort them from longest to shortest: | |||
<PRE CLASS="verbatim">def sort_by_length(words): | |||
t = [] | t = [] | ||
for word in words: | for word in words: | ||
| Line 271: | Line 451: | ||
res.append(word) | res.append(word) | ||
return res | return res | ||
</PRE> | |||
tuple is a word preceded by its length. | The first loop builds a list of tuples, where each | ||
tuple is a word preceded by its length. | |||
<TT>sort</TT> compares the first element, length, first, and | |||
only considers the second element to break ties. The keyword argument | only considers the second element to break ties. The keyword argument | ||
<TT>reverse=True</TT> tells <TT>sort</TT> to go in decreasing order. | |||
words in descending order of length. | |||
The second loop traverses the list of tuples and builds a list of | |||
words in descending order of length. | |||
<DIV CLASS="theorem">'''Exercise 2'''  '' | |||
In this example, ties are broken by comparing words, so words | In this example, ties are broken by comparing words, so words | ||
with the same length appear in alphabetical order. For other | with the same length appear in alphabetical order. For other | ||
applications you might want to break ties at random. Modify | applications you might want to break ties at random. Modify | ||
this example so that words with the same length appear in | this example so that words with the same length appear in | ||
random order. Hint: see the | random order. Hint: see the ''''<TT>random</TT>'''' function in the | ||
''''<TT>random</TT>'''' module.'' | |||
'' | |||
'''' | |||
'''' | |||
'' | |||
</DIV>=== 12.8  Sequences of sequences === | |||
I have focused on lists of tuples, but almost all of the examples in | |||
this chapter also work with lists of lists, tuples of tuples, and | this chapter also work with lists of lists, tuples of tuples, and | ||
tuples of lists. To avoid enumerating the possible combinations, it | tuples of lists. To avoid enumerating the possible combinations, it | ||
is sometimes easier to talk about sequences of sequences. | is sometimes easier to talk about sequences of sequences. | ||
In many contexts, the different kinds of sequences (strings, lists and | |||
tuples) can be used interchangeably. So how and why do you choose one | tuples) can be used interchangeably. So how and why do you choose one | ||
over the others? | over the others? | ||
To start with the obvious, strings are more limited than other | |||
sequences because the elements have to be characters. They are | sequences because the elements have to be characters. They are | ||
also immutable. If you need the ability to change the characters | also immutable. If you need the ability to change the characters | ||
in a string (as opposed to creating a new string), you might | in a string (as opposed to creating a new string), you might | ||
want to use a list of characters instead. | want to use a list of characters instead. | ||
But there are a few cases where you might prefer tuples: | |||
Lists are more common than tuples, mostly because they are mutable. | |||
But there are a few cases where you might prefer tuples: | |||
*In some contexts, like a <TT>return</TT> statement, it is | |||
syntactically simpler to create a tuple than a list. In other | syntactically simpler to create a tuple than a list. In other | ||
contexts, you might prefer a list. | contexts, you might prefer a list. | ||
have to use an immutable type like a tuple or string. | |||
*If you want to use a sequence as a dictionary key, you | |||
have to use an immutable type like a tuple or string. | |||
*If you are passing a sequence as an argument to a function, | |||
using tuples reduces the potential for unexpected behavior | using tuples reduces the potential for unexpected behavior | ||
due to aliasing. | due to aliasing. | ||
like | |||
But Python provides the built-in functions | Because tuples are immutable, they don’t provide methods | ||
and | like <TT>sort</TT> and <TT>reverse</TT>, which modify existing lists. | ||
But Python provides the built-in functions <TT>sorted</TT> | |||
and <TT>reversed</TT>, which take any sequence as a parameter | |||
and return a new list with the same elements in a different | and return a new list with the same elements in a different | ||
order. | order. | ||
=== 12.9  Debugging === | |||
structures | |||
Lists, dictionaries and tuples are known generically as '''data | |||
structures'''; in this chapter we are starting to see compound data | |||
structures, like lists of tuples, and dictionaries that contain tuples | structures, like lists of tuples, and dictionaries that contain tuples | ||
as keys and lists as values. Compound data structures are useful, but | as keys and lists as values. Compound data structures are useful, but | ||
they are prone to what I call | they are prone to what I call '''shape errors'''; that is, errors | ||
caused when a data structure has the wrong type, size or composition. | caused when a data structure has the wrong type, size or composition. | ||
For example, if you are expecting a list with one integer and I | For example, if you are expecting a list with one integer and I | ||
give you a plain old integer (not in a list), it won’t work. | give you a plain old integer (not in a list), it won’t work. | ||
called | |||
To help debug these kinds of errors, I have written a module | |||
called <TT>structshape</TT> that provides a function, also called | |||
<TT>structshape</TT>, that takes any kind of data structure as | |||
an argument and returns a string that summarizes its shape. | an argument and returns a string that summarizes its shape. | ||
You can download it from | You can download it from <TT>thinkpython.com/code/structshape.py</TT> | ||
Here’s the result for a simple list: | |||
<PRE CLASS="verbatim">>>> from structshape import structshape | |||
>>> t = [1,2,3] | >>> t = [1,2,3] | ||
>>> print structshape(t) | >>> print structshape(t) | ||
list of 3 int | list of 3 int | ||
</PRE> | |||
was easier not to deal with plurals. Here’s a list of lists: | A fancier program might write “list of 3 int''s'',” but it | ||
was easier not to deal with plurals. Here’s a list of lists: | |||
<PRE CLASS="verbatim">>>> t2 = [[1,2], [3,4], [5,6]] | |||
>>> print structshape(t2) | >>> print structshape(t2) | ||
list of 3 list of 2 int | list of 3 list of 2 int | ||
</PRE> | |||
If the elements of the list are not the same type, | |||
<TT>structshape</TT> groups them, in order, by type: | |||
<PRE CLASS="verbatim">>>> t3 = [1, 2, 3, 4.0, '5', '6', [7], [8], 9] | |||
>>> print structshape(t3) | >>> print structshape(t3) | ||
list of (3 int, float, 2 str, 2 list of int, int) | list of (3 int, float, 2 str, 2 list of int, int) | ||
</PRE> | |||
Here’s a list of tuples: | |||
<PRE CLASS="verbatim">>>> s = 'abc' | |||
>>> lt = zip(t, s) | >>> lt = zip(t, s) | ||
>>> print structshape(lt) | >>> print structshape(lt) | ||
list of 3 tuple of (int, str) | list of 3 tuple of (int, str) | ||
</PRE> | |||
And here’s a dictionary with 3 items that map integers to strings. | |||
<PRE CLASS="verbatim">>>> d = dict(lt) | |||
>>> print structshape(d) | >>> print structshape(d) | ||
dict of 3 int->str | dict of 3 int->str | ||
</PRE> | |||
If you are having trouble keeping track of your data structures, | |||
<TT>structshape</TT> can help. | |||
=== 12.10  Glossary === | |||
<DL CLASS="description"><DT CLASS="dt-description">'''tuple:'''</DT><DD CLASS="dd-description"> An immutable sequence of elements. | |||
</DD><DT CLASS="dt-description">'''tuple assignment:'''</DT><DD CLASS="dd-description"> An assignment with a sequence on the | |||
right side and a tuple of variables on the left. The right | right side and a tuple of variables on the left. The right | ||
side is evaluated and then its elements are assigned to the | side is evaluated and then its elements are assigned to the | ||
variables on the left. | variables on the left. | ||
</DD><DT CLASS="dt-description">'''gather:'''</DT><DD CLASS="dd-description"> The operation of assembling a variable-length | |||
argument tuple. | argument tuple. | ||
</DD><DT CLASS="dt-description">'''scatter:'''</DT><DD CLASS="dd-description"> The operation of treating a sequence as a list of | |||
arguments. | arguments. | ||
</DD><DT CLASS="dt-description">'''DSU:'''</DT><DD CLASS="dd-description"> Abbreviation of “decorate-sort-undecorate,” a | |||
pattern that involves building a list of tuples, sorting, and | pattern that involves building a list of tuples, sorting, and | ||
extracting part of the result. | extracting part of the result. | ||
</DD><DT CLASS="dt-description">'''data structure:'''</DT><DD CLASS="dd-description"> A collection of related values, often | |||
organized in lists, dictionaries, tuples, etc. | organized in lists, dictionaries, tuples, etc. | ||
</DD><DT CLASS="dt-description">'''shape (of a data structure):'''</DT><DD CLASS="dd-description"> A summary of the type, | |||
size and composition of a data structure. | size and composition of a data structure. | ||
</DD></DL>=== 12.11  Exercises === | |||
Write a function called | |||
<DIV CLASS="theorem">'''Exercise 3'''  '' | |||
Write a function called ''<CODE>''most_frequent''</CODE>'' that takes a string and | |||
prints the letters in decreasing order of frequency. Find text | prints the letters in decreasing order of frequency. Find text | ||
samples from several different languages and see how letter frequency | samples from several different languages and see how letter frequency | ||
varies between languages. Compare your results with the tables at | varies between languages. Compare your results with the tables at | ||
''''<TT>wikipedia.org/wiki/Letter_frequencies</TT>''''.'' | |||
'' | |||
'' | |||
</DIV><DIV CLASS="theorem">'''Exercise 4'''  '' | |||
that reads a word list from a file (see Section  | '' | ||
prints all the sets of words that are anagrams. | '' | ||
'' | |||
''More anagrams!'' | |||
*''Write a program | |||
that reads a word list from a file (see Section ''''9.1'''') and | |||
prints all the sets of words that are anagrams.'' | |||
''Here is an example of what the output might look like:'' | |||
<PRE CLASS="verbatim">''['deltas', 'desalt', 'lasted', 'salted', 'slated', 'staled'] | |||
['retainers', 'ternaries'] | ['retainers', 'ternaries'] | ||
['generating', 'greatening'] | ['generating', 'greatening'] | ||
['resmelts', 'smelters', 'termless'] | ['resmelts', 'smelters', 'termless'] | ||
''</PRE> | |||
''Hint: you might want to build a dictionary that maps from a | |||
set of letters to a list of words that can be spelled with those | set of letters to a list of words that can be spelled with those | ||
letters. The question is, how can you represent the set of | letters. The question is, how can you represent the set of | ||
letters in a way that can be used as a key? | letters in a way that can be used as a key?'' | ||
of anagrams first, followed by the second largest set, and so on. | |||
*''Modify the previous program so that it prints the largest set | |||
of anagrams first, followed by the second largest set, and so on.'' | |||
'' | |||
'' | |||
*''In Scrabble a “bingo” is when you play all seven tiles in | |||
your rack, along with a letter on the board, to form an eight-letter | your rack, along with a letter on the board, to form an eight-letter | ||
word. What set of 8 letters forms the most possible bingos? | word. What set of 8 letters forms the most possible bingos? | ||
Hint: there are seven. | Hint: there are seven.'' | ||
into the other by swapping two letters | |||
*''''Two words form a “metathesis pair” if you can transform one | |||
into the other by swapping two letters''''<SUP>''''3''''</SUP>''''; for example, | |||
“converse” and “conserve.” Write a program that finds all of | “converse” and “conserve.” Write a program that finds all of | ||
the metathesis pairs in the dictionary. Hint: don’t test all pairs | the metathesis pairs in the dictionary. Hint: don’t test all pairs | ||
of words, and don’t test all possible swaps. | of words, and don’t test all possible swaps.'''' | ||
''''You can download a solution from ''''''''<TT>thinkpython.com/code/anagram_sets.py</TT>''''''''.'''' | |||
</DIV><DIV CLASS="theorem">'''Exercise 5'''   | |||
'' | |||
'' | |||
''Here’s another Car Talk Puzzler''<SUP>''4''</SUP>'':'' | |||
<BLOCKQUOTE CLASS="quote">'' | |||
What is the longest English word, that remains a valid English word, | What is the longest English word, that remains a valid English word, | ||
as you remove its letters one at a time? | as you remove its letters one at a time?'' | ||
''Now, letters can be removed from either end, or the middle, but you | |||
can’t rearrange any of the letters. Every time you drop a letter, you | can’t rearrange any of the letters. Every time you drop a letter, you | ||
wind up with another English word. If you do that, you’re eventually | wind up with another English word. If you do that, you’re eventually | ||
| Line 401: | Line 664: | ||
English word—one that’s found in the dictionary. I want to know | English word—one that’s found in the dictionary. I want to know | ||
what’s the longest word and how many letters does it | what’s the longest word and how many letters does it | ||
have? | have?'' | ||
''I’m going to give you a little modest example: Sprite. Ok? You start | |||
off with sprite, you take a letter off, one from the interior of the | off with sprite, you take a letter off, one from the interior of the | ||
word, take the r away, and we’re left with the word spite, then we | word, take the r away, and we’re left with the word spite, then we | ||
take the e off the end, we’re left with spit, we take the s off, we’re | take the e off the end, we’re left with spit, we take the s off, we’re | ||
left with pit, it, and I. | left with pit, it, and I. | ||
'' | |||
</BLOCKQUOTE> | |||
and then find the longest one. | '' | ||
some suggestions: | '' | ||
''Write a program to find all words that can be reduced in this way, | |||
and then find the longest one.'' | |||
''This exercise is a little more challenging than most, so here are | |||
some suggestions:'' | |||
*''You might want to write a function that takes a word and | |||
computes a list of all the words that can be formed by removing one | computes a list of all the words that can be formed by removing one | ||
letter. These are the “children” of the word. | letter. These are the “children” of the word.'' | ||
'' | |||
'' | |||
*''Recursively, a word is reducible if any of its children | |||
are reducible. As a base case, you can consider the empty | are reducible. As a base case, you can consider the empty | ||
string reducible. | string reducible.'' | ||
*''The wordlist I provided, ''''<TT>words.txt</TT>'''', doesn’t | |||
contain single letter words. So you might want to add | contain single letter words. So you might want to add | ||
“I”, “a”, and the empty string. | “I”, “a”, and the empty string.'' | ||
to memoize the words that are known to be reducible. | |||
*''To improve the performance of your program, you might want | |||
to memoize the words that are known to be reducible.'' | |||
''You can see my solution at ''''<TT>thinkpython.com/code/reducible.py</TT>''''.'' | |||
</DIV><HR CLASS="footnoterule"><DL CLASS="thefootnotes"><DT CLASS="dt-thefootnotes"> | |||
1</DT><DD CLASS="dd-thefootnotes">In Python 3.0, <TT>zip</TT> returns an | |||
iterator of tuples, but for most purposes, an iterator behaves like | iterator of tuples, but for most purposes, an iterator behaves like | ||
a list. | a list. | ||
</DD><DT CLASS="dt-thefootnotes">2</DT><DD CLASS="dd-thefootnotes">This behavior is | |||
slightly different in Python 3.0. | slightly different in Python 3.0. | ||
</DD><DT CLASS="dt-thefootnotes">3</DT><DD CLASS="dd-thefootnotes">This exercise is | |||
inspired by an example at <TT>puzzlers.org</TT>. | inspired by an example at <TT>puzzlers.org</TT>. | ||
</DD><DT CLASS="dt-thefootnotes">4</DT><DD CLASS="dd-thefootnotes"> | |||
<TT>www.cartalk.com/content/puzzler/transcripts/200651</TT> | <TT>www.cartalk.com/content/puzzler/transcripts/200651</TT> | ||
</DD></DL> | |||
<HR> | <HR> | ||
<IMG SRC="previous_motif.gif" ALT="Previous"> | |||
<IMG SRC="contents_motif.gif" ALT="Up"> | |||
<IMG SRC="next_motif.gif" ALT="Next"> | |||
Revision as of 23:09, 15 September 2008
Chapter 12 Tuples
12.1 Tuples are immutable
A tuple is a sequence of values. The values can be any type, and they are indexed by integers, so in that respect tuples are a lot like lists. The important difference is that tuples are immutable.
Syntactically, a tuple is a comma-separated list of values:
>>> t = 'a', 'b', 'c', 'd', 'e'
Although it is not necessary, it is common to enclose tuples in parentheses:
>>> t = ('a', 'b', 'c', 'd', 'e')
To create a tuple with a single element, you have to include the final comma:
>>> t1 = ('a',)
>>> type(t1)
<type 'tuple'>
Without the comma, Python treats ('a') as a string in
parentheses:
>>> t2 = ('a')
>>> type(t2)
<type 'str'>
Another way to create a tuple is the built-in function tuple. With no argument, it creates an empty tuple:
>>> t = tuple() >>> print t ()
If the argument is a sequence (string, list or tuple), the result is a tuple with the elements of the sequence:
>>> t = tuple('lupins')
>>> print t
('l', 'u', 'p', 'i', 'n', 's')
Because tuple is the name of a built-in function, you should avoid using it as a variable name.
Most list operators also work on tuples. The bracket operator indexes an element:
>>> t = ('a', 'b', 'c', 'd', 'e')
>>> print t[0]
'a'
And the slice operator selects a range of elements.
>>> print t[1:3]
('b', 'c')
But if you try to modify one of the elements of the tuple, you get an error:
>>> t[0] = 'A' TypeError: object doesn't support item assignment
You can’t modify the elements of a tuple, but you can replace one tuple with another:
>>> t = ('A',) + t[1:]
>>> print t
('A', 'b', 'c', 'd', 'e')
=== 12.2 Tuple assignment ===
It is often useful to swap the values of two variables. With conventional assignments, you have to use a temporary variable. For example, to swap a and b:
>>> temp = a >>> a = b >>> b = temp
This solution is cumbersome; tuple assignment is more elegant:
>>> a, b = b, a
The left side is a tuple of variables; the right side is a tuple of expressions. Each value is assigned to its respective variable. All the expressions on the right side are evaluated before any of the assignments.
The number of variables on the left and the number of values on the right have to be the same:
>>> a, b = 1, 2, 3 ValueError: too many values to unpack
More generally, the right side can be any kind of sequence (string, list or tuple). For example, to split an email address into a user name and a domain, you could write:
>>> addr = 'monty@python.org'
>>> uname, domain = addr.split('@')
The return value from split is a list with two elements; the first element is assigned to uname, the second to domain.
>>> print uname monty >>> print domain python.org
=== 12.3 Tuples as return values ===
Strictly speaking, a function can only return one value, but
if the value is a tuple, the effect is the same as returning
multiple values. For example, if you want to divide two integers
and compute the quotient and remainder, it is inefficient to
compute x/y and then x%y. It is better to compute
them both at the same time.
The built-in function divmod takes two arguments and returns a tuple of two values, the quotient and remainder. You can store the result as a tuple:
>>> t = divmod(7, 3) >>> print t (2, 1)
Or use tuple assignment to store the elements separately:
>>> quot, rem = divmod(7, 3) >>> print quot 2 >>> print rem 1
Here is an example of a function that returns a tuple:
def min_max(t):
return min(t), max(t)
max and min are built-in functions that find
the largest and smallest elements of a sequence. min_max
computes both and returns a tuple of two values.
12.4 Variable-length argument tuples
Functions can take a variable number of arguments. A parameter name that begins with * gathers arguments into a tuple. For example, printall takes any number of arguments and prints them:
def printall(*args):
print args
The gather parameter can have any name you like, but args is conventional. Here’s how the function works:
>>> printall(1, 2.0, '3') (1, 2.0, '3')
You can combine the gather operator with required and positional arguments:
def pointless(required, optional=0, *args):
print required, optional, args
Run this function with 1, 2, 3 and 4 or more arguments and make sure you understand what it does.
The complement of gather is scatter. If you have a
sequence of values and you want to pass it to a function
as multiple arguments, you can use the * operator.
For example, divmod takes exactly two arguments; it
doesn’t work with a tuple:
>>> t = (7, 3) >>> divmod(t) TypeError: divmod expected 2 arguments, got 1
But if you scatter the tuple, it works:
>>> divmod(*t) (2, 1)
Many of the built-in functions use variable-length argument tuples. For example, 'max' and 'min' can take any number of arguments: ' '
''>>> max(1,2,3) 3 ''
But 'sum' does not.
''>>> sum(1,2,3) TypeError: sum expected at most 2 arguments, got 3 ''
Write a function called 'sumall' that takes any number of arguments and returns their sum.
=== 12.5 Lists and tuples ===
zip is a built-in function that takes two or more sequences and
“zips” them into a list1 of tuples where each tuple contains one element from each
sequence.
This example zips a string and a list:
>>> s = 'abc'
>>> t = [0, 1, 2]
>>> zip(s, t)
[('a', 0), ('b', 1), ('c', 2)]
The result is a list of tuples where each tuple contains a character from the string and the corresponding element from the list.
If the sequences are not the same length, the result has the length of the shorter one.
>>> zip('Anne', 'Elk')
[('A', 'E'), ('n', 'l'), ('n', 'k')]
You can use tuple assignment in a for loop to traverse a list of tuples:
t = [('a', 0), ('b', 1), ('c', 2)]
for letter, number in t:
print number, letter
Each time through the loop, Python selects the next tuple in the list and assigns the elements to letter and number. The output of this loop is:
0 a 1 b 2 c
If you combine zip, for and tuple assignment, you get a
useful idiom for traversing two (or more) sequences at the same
time. For example, has_match takes two sequences, t1 and
t2, and returns True if there is an index i
such that t1[i] == t2[i]:
def has_match(t1, t2):
for x, y in zip(t1, t2):
if x == y:
return True
return False
If you need to traverse the elements of a sequence and their indices, you can use the built-in function enumerate:
for index, element in enumerate('abc'):
print index, element
The output of this loop is:
0 a 1 b 2 c
Again.
12.6 Dictionaries and tuples
Dictionaries have a method called items that returns a list of tuples, where each tuple is a key-value pair2.
>>> d = {'a':0, 'b':1, 'c':2}
>>> t = d.items()
>>> print t
[('a', 0), ('c', 2), ('b', 1)]
As you should expect from a dictionary, the items are in no particular order.
Conversely, you can use a list of tuples to initialize a new dictionary:
>>> t = [('a', 0), ('c', 2), ('b', 1)]
>>> d = dict(t)
>>> print d
{'a': 0, 'c': 2, 'b': 1}
Combining dict with zip yields a concise way to create a dictionary:
>>> d = dict(zip('abc', range(3)))
>>> print d
{'a': 0, 'c': 2, 'b': 1}
The dictionary method update also takes a list of tuples and adds them, as key-value pairs, to an existing dictionary.
Combining items, tuple assignment and for, you get the idiom for traversing the keys and values of a dictionary:
for key, val in d.items():
print val, key
The output of this loop is:
0 a 2 c 1 b
Again.
It is common to use tuples as keys in dictionaries (primarily because
you can’t use lists). For example, a telephone directory might map
from last-name, first-name pairs to telephone numbers. Assuming
that we have defined last, first and number, we
could write:
directory[last,first] = number
The expression in brackets is a tuple. We could use tuple assignment to traverse this dictionary.
for last, first in directory:
print first, last, directory[last,first]
This loop traverses the keys in directory, which are tuples. It assigns the elements of each tuple to last and first, then prints the name and corresponding telephone number.
There are two ways to represent tuples in a state diagram. The more
detailed version shows the indices and elements just as they appear in
a list. For example, the tuple ('Cleese', 'John') would appear:
But in a larger diagram you might want to leave out the details. For example, a diagram of the telephone directory might appear:
Here the tuples are shown using Python syntax as a graphical shorthand.
The telephone number in the diagram is the complaints line for the BBC, so please don’t call it.
12.7 Comparing tuples
The comparison operators work with tuples and other sequences; Python starts by comparing the first element from each sequence. If they are equal, it goes on to the next elements, and so on, until it finds elements that differ. Subsequent elements are not considered (even if they are really big).
>>> (0, 1, 2) < (0, 3, 4) True >>> (0, 1, 2000000) < (0, 3, 4) True
The sort function works the same way. It sorts primarily by first element, but in the case of a tie, it sorts by second element, and so on.
This feature lends itself to a pattern called DSU for
- Decorate
- a sequence by building a list of tuples with one or more sort keys preceding the elements from the sequence,
- Sort
- the list of tuples, and
- Undecorate
- by extracting the sorted elements of the sequence.
For example, suppose you have a list of words and you want to
sort them from longest to shortest:
def sort_by_length(words):
t = []
for word in words:
t.append((len(word), word))
t.sort(reverse=True)
res = []
for length, word in t:
res.append(word)
return res
The first loop builds a list of tuples, where each tuple is a word preceded by its length.
sort compares the first element, length, first, and only considers the second element to break ties. The keyword argument reverse=True tells sort to go in decreasing order.
The second loop traverses the list of tuples and builds a list of words in descending order of length.
In this example, ties are broken by comparing words, so words with the same length appear in alphabetical order. For other applications you might want to break ties at random. Modify this example so that words with the same length appear in random order. Hint: see the 'random' function in the 'random' module. ' '
=== 12.8 Sequences of sequences ===
I have focused on lists of tuples, but almost all of the examples in
this chapter also work with lists of lists, tuples of tuples, and
tuples of lists. To avoid enumerating the possible combinations, it
is sometimes easier to talk about sequences of sequences.
In many contexts, the different kinds of sequences (strings, lists and tuples) can be used interchangeably. So how and why do you choose one over the others?
To start with the obvious, strings are more limited than other sequences because the elements have to be characters. They are also immutable. If you need the ability to change the characters in a string (as opposed to creating a new string), you might want to use a list of characters instead.
Lists are more common than tuples, mostly because they are mutable. But there are a few cases where you might prefer tuples:
- In some contexts, like a return statement, it is
syntactically simpler to create a tuple than a list. In other contexts, you might prefer a list.
- If you want to use a sequence as a dictionary key, you
have to use an immutable type like a tuple or string.
- If you are passing a sequence as an argument to a function,
using tuples reduces the potential for unexpected behavior due to aliasing.
Because tuples are immutable, they don’t provide methods like sort and reverse, which modify existing lists. But Python provides the built-in functions sorted and reversed, which take any sequence as a parameter and return a new list with the same elements in a different order.
12.9 Debugging
Lists, dictionaries and tuples are known generically as data structures; in this chapter we are starting to see compound data structures, like lists of tuples, and dictionaries that contain tuples as keys and lists as values. Compound data structures are useful, but they are prone to what I call shape errors; that is, errors caused when a data structure has the wrong type, size or composition. For example, if you are expecting a list with one integer and I give you a plain old integer (not in a list), it won’t work.
To help debug these kinds of errors, I have written a module
called structshape that provides a function, also called
structshape, that takes any kind of data structure as
an argument and returns a string that summarizes its shape.
You can download it from thinkpython.com/code/structshape.py
Here’s the result for a simple list:
>>> from structshape import structshape >>> t = [1,2,3] >>> print structshape(t) list of 3 int
A fancier program might write “list of 3 ints,” but it was easier not to deal with plurals. Here’s a list of lists:
>>> t2 = [[1,2], [3,4], [5,6]] >>> print structshape(t2) list of 3 list of 2 int
If the elements of the list are not the same type, structshape groups them, in order, by type:
>>> t3 = [1, 2, 3, 4.0, '5', '6', [7], [8], 9] >>> print structshape(t3) list of (3 int, float, 2 str, 2 list of int, int)
Here’s a list of tuples:
>>> s = 'abc' >>> lt = zip(t, s) >>> print structshape(lt) list of 3 tuple of (int, str)
And here’s a dictionary with 3 items that map integers to strings.
>>> d = dict(lt) >>> print structshape(d) dict of 3 int->str
If you are having trouble keeping track of your data structures, structshape can help.
12.10 Glossary
- tuple:
- An immutable sequence of elements.
- tuple assignment:
- An assignment with a sequence on the right side and a tuple of variables on the left. The right side is evaluated and then its elements are assigned to the variables on the left.
- gather:
- The operation of assembling a variable-length argument tuple.
- scatter:
- The operation of treating a sequence as a list of arguments.
- DSU:
- Abbreviation of “decorate-sort-undecorate,” a pattern that involves building a list of tuples, sorting, and extracting part of the result.
- data structure:
- A collection of related values, often organized in lists, dictionaries, tuples, etc.
- shape (of a data structure):
- A summary of the type, size and composition of a data structure.
=== 12.11 Exercises ===
Write a function called most_frequent that takes a string and
prints the letters in decreasing order of frequency. Find text
samples from several different languages and see how letter frequency
varies between languages. Compare your results with the tables at
'wikipedia.org/wiki/Letter_frequencies'.
More anagrams!
- Write a program
that reads a word list from a file (see Section '9.1') and prints all the sets of words that are anagrams. Here is an example of what the output might look like:
''['deltas', 'desalt', 'lasted', 'salted', 'slated', 'staled'] ['retainers', 'ternaries'] ['generating', 'greatening'] ['resmelts', 'smelters', 'termless'] ''
Hint: you might want to build a dictionary that maps from a set of letters to a list of words that can be spelled with those letters. The question is, how can you represent the set of letters in a way that can be used as a key?
- Modify the previous program so that it prints the largest set
of anagrams first, followed by the second largest set, and so on.
- In Scrabble a “bingo” is when you play all seven tiles in
your rack, along with a letter on the board, to form an eight-letter word. What set of 8 letters forms the most possible bingos? Hint: there are seven.
- 'Two words form a “metathesis pair” if you can transform one
into the other by swapping two letters''3''; for example, “converse” and “conserve.” Write a program that finds all of the metathesis pairs in the dictionary. Hint: don’t test all pairs of words, and don’t test all possible swaps.' 'You can download a solution from '''thinkpython.com/code/anagram_sets.py'''.'
Here’s another Car Talk Puzzler4:
What is the longest English word, that remains a valid English word, as you remove its letters one at a time? Now, letters can be removed from either end, or the middle, but you can’t rearrange any of the letters. Every time you drop a letter, you wind up with another English word. If you do that, you’re eventually going to wind up with one letter and that too is going to be an English word—one that’s found in the dictionary. I want to know what’s the longest word and how many letters does it have?
I’m going to give you a little modest example: Sprite. Ok? You start off with sprite, you take a letter off, one from the interior of the word, take the r away, and we’re left with the word spite, then we take the e off the end, we’re left with spit, we take the s off, we’re left with pit, it, and I.
Write a program to find all words that can be reduced in this way, and then find the longest one.
This exercise is a little more challenging than most, so here are some suggestions:
- You might want to write a function that takes a word and
computes a list of all the words that can be formed by removing one letter. These are the “children” of the word.
- Recursively, a word is reducible if any of its children
are reducible. As a base case, you can consider the empty string reducible.
- The wordlist I provided, 'words.txt', doesn’t
contain single letter words. So you might want to add “I”, “a”, and the empty string.
- To improve the performance of your program, you might want
to memoize the words that are known to be reducible.
You can see my solution at 'thinkpython.com/code/reducible.py'.
- 1
- In Python 3.0, zip returns an iterator of tuples, but for most purposes, an iterator behaves like a list.
- 2
- This behavior is slightly different in Python 3.0.
- 3
- This exercise is inspired by an example at puzzlers.org.
- 4
- www.cartalk.com/content/puzzler/transcripts/200651
<IMG SRC="previous_motif.gif" ALT="Previous"> <IMG SRC="contents_motif.gif" ALT="Up"> <IMG SRC="next_motif.gif" ALT="Next">