Jump to content

Archive:Think Python/Lists

From IdeaWazaWiki
Revision as of 22:43, 15 September 2008 by wikademia>Whiteknight (Think Python: Automatically uploading HTML source of this book from http://www.greenteapress.com/thinkpython/html/. Will convert to wikitext in a separate step)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"

           "http://www.w3.org/TR/REC-html40/loose.dtd">

<HTML> <HEAD>

<META http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <META name="GENERATOR" content="hevea 1.10"> <LINK rel="stylesheet" type="text/css" href="book.css"> <TITLE>Lists</TITLE> </HEAD> <BODY > <A HREF="book010.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A> <A HREF="index.html"><IMG SRC="contents_motif.gif" ALT="Up"></A> <A HREF="book012.html"><IMG SRC="next_motif.gif" ALT="Next"></A>


<A NAME="htoc116">Chapter 10</A>  Lists

<A NAME="@default719"></A> <A NAME="@default720"></A>

<A NAME="toc105"></A><A NAME="htoc117">10.1</A>  A list is a sequence

Like a string, a list is a sequence of values. In a string, the

values are characters; in a list, they can be any type. The values in

list are called elements or sometimes items.

<A NAME="@default721"></A>

<A NAME="@default722"></A>

<A NAME="@default723"></A>

There are several ways to create a new list; the simplest is to enclose the elements in square brackets ([ and ]):

<FONT COLOR=blue><FONT SIZE=4>[10, 20, 30, 40]
['crunchy frog', 'ram bladder', 'lark vomit']
</FONT></FONT>

The first example is a list of four integers. The second is a list of

three strings. The elements of a list don’t have to be the same type. The following list contains a string, a float, an integer, and

(lo!) another list:

<FONT COLOR=blue><FONT SIZE=4>['spam', 2.0, 5, [10, 20]]
</FONT></FONT>

A list within another list is nested.

<A NAME="@default724"></A> <A NAME="@default725"></A>

A list that contains no elements is

called an empty list; you can create one with empty

brackets, [].

<A NAME="@default726"></A> <A NAME="@default727"></A>

As you might expect, you can assign list values to variables:

<FONT COLOR=blue><FONT SIZE=4>>>> cheeses = ['Cheddar', 'Edam', 'Gouda']
>>> numbers = [17, 123]
>>> empty = []
>>> print cheeses, numbers, empty
['Cheddar', 'Edam', 'Gouda'] [17, 123] []
</FONT></FONT>

<A NAME="@default728"></A>

<A NAME="toc106"></A><A NAME="htoc118">10.2</A>  Lists are mutable

<A NAME="@default729"></A>

<A NAME="@default730"></A> <A NAME="@default731"></A> <A NAME="@default732"></A>

<A NAME="@default733"></A>

The syntax for accessing the elements of a list is the same as for

accessing the characters of a string—the bracket operator. The expression inside the brackets specifies the index. Remember that the

indices start at 0:

<FONT COLOR=blue><FONT SIZE=4>>>> print cheeses[0]
Cheddar
</FONT></FONT>

Unlike strings, lists are mutable. When the bracket operator appears

on the left side of an assignment, it identifies the element of the

list that will be assigned.

<A NAME="@default734"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> numbers = [17, 123]
>>> numbers[1] = 5
>>> print numbers
[17, 5]
</FONT></FONT>

The one-eth element of numbers, which used to be 123, is now 5.

<A NAME="@default735"></A> <A NAME="@default736"></A>

You can think of a list as a relationship between indices and

elements. This relationship is called a mapping; each index

“maps to” one of the elements. Here is a state diagram showing cheeses, numbers and empty:

<A NAME="@default737"></A>

<A NAME="@default738"></A>

<A NAME="@default739"></A>

<IMG SRC="book013.png">

Lists are represented by boxes with the word “list” outside

and the elements of the list inside. cheeses refers to a list with three elements indexed 0, 1 and 2. numbers contains two elements; the diagram shows that the value of the second element has been reassigned from 123 to 5.

empty refers to a list with no elements.

<A NAME="@default740"></A> <A NAME="@default741"></A>

List indices work the same way as string indices:

  • Any integer expression can be used as an index.
  • If you try to read or write an element that does not exist, you get an IndexError.

    <A NAME="@default742"></A> <A NAME="@default743"></A>

  • If an index has a negative value, it counts backward from the end of the list.

<A NAME="@default744"></A>

<A NAME="@default745"></A>

<A NAME="@default746"></A> <A NAME="@default747"></A>

<A NAME="@default748"></A>

The in operator also works on lists.

<FONT COLOR=blue><FONT SIZE=4>>>> cheeses = ['Cheddar', 'Edam', 'Gouda']
>>> 'Edam' in cheeses
True
>>> 'Brie' in cheeses
False
</FONT></FONT>

<A NAME="toc107"></A><A NAME="htoc119">10.3</A>  Traversing a list

<A NAME="@default749"></A> <A NAME="@default750"></A> <A NAME="@default751"></A> <A NAME="@default752"></A>

<A NAME="@default753"></A>

The most common way to traverse the elements of a list is with a for loop. The syntax is the same as for strings:

<FONT COLOR=blue><FONT SIZE=4>for cheese in cheeses:
    print cheese
</FONT></FONT>

This works well if you only need to read the elements of the

list. But if you want to write or update the elements, you need the indices. A common way to do that is to combine

the functions range and len:

<A NAME="@default754"></A> <A NAME="@default755"></A>

<FONT COLOR=blue><FONT SIZE=4>for i in range(len(numbers)):
    numbers[i] = numbers[i] * 2
</FONT></FONT>

This loop traverses the list and updates each element. len

returns the number of elements in the list. range returns a list of indices from 0 to n−1, where n is the length of the list. Each time through the loop i gets the index of the next element. The assignment statement in the body uses i to read the old value of the element and to assign the

new value.

<A NAME="@default756"></A> <A NAME="@default757"></A>

A for loop over an empty list never executes the body:

<FONT COLOR=blue><FONT SIZE=4>for x in empty:
    print 'This never happens.'
</FONT></FONT>

Although a list can contain another list, the nested

list still counts as a single element. The length of this list is

four:

<A NAME="@default758"></A> <A NAME="@default759"></A>

<FONT COLOR=blue><FONT SIZE=4>['spam', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]
</FONT></FONT>

<A NAME="toc108"></A><A NAME="htoc120">10.4</A>  List operations

<A NAME="@default760"></A>

The + operator concatenates lists:

<A NAME="@default761"></A> <A NAME="@default762"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print c
[1, 2, 3, 4, 5, 6]
</FONT></FONT>

Similarly, the * operator repeats a list a given number of times:

<A NAME="@default763"></A> <A NAME="@default764"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> [0] * 4
[0, 0, 0, 0]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
</FONT></FONT>

The first example repeats [0] four times. The second example repeats the list [1, 2, 3] three times.

<A NAME="toc109"></A><A NAME="htoc121">10.5</A>  List slices

<A NAME="@default765"></A>

<A NAME="@default766"></A> <A NAME="@default767"></A> <A NAME="@default768"></A>

<A NAME="@default769"></A>

The slice operator also works on lists:

<FONT COLOR=blue><FONT SIZE=4>>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> t[1:3]
['b', 'c']
>>> t[:4]
['a', 'b', 'c', 'd']
>>> t[3:]
['d', 'e', 'f']
</FONT></FONT>

If you omit the first index, the slice starts at the beginning.

If you omit the second, the slice goes to the end. So if you

omit both, the slice is a copy of the whole list.

<A NAME="@default770"></A>

<A NAME="@default771"></A>

<A NAME="@default772"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> t[:]
['a', 'b', 'c', 'd', 'e', 'f']
</FONT></FONT>

Since lists are mutable, it is often useful to make a copy

before performing operations that fold, spindle or mutilate

lists.

<A NAME="@default773"></A>

A slice operator on the left side of an assignment can update multiple elements:

<A NAME="@default774"></A> <A NAME="@default775"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> t[1:3] = ['x', 'y']
>>> print t
['a', 'x', 'y', 'd', 'e', 'f']
</FONT></FONT>

<A NAME="toc110"></A><A NAME="htoc122">10.6</A>  List methods

<A NAME="@default776"></A> <A NAME="@default777"></A>

Python provides methods that operate on lists. For example, append adds a new element to the end of a list:

<A NAME="@default778"></A> <A NAME="@default779"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> t = ['a', 'b', 'c']
>>> t.append('d')
>>> print t
['a', 'b', 'c', 'd']
</FONT></FONT>

extend takes a list as an argument and appends all of the elements:

<A NAME="@default780"></A> <A NAME="@default781"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> t1 = ['a', 'b', 'c']
>>> t2 = ['d', 'e']
>>> t1.extend(t2)
>>> print t1
['a', 'b', 'c', 'd', 'e']
</FONT></FONT>

This example leaves t2 unmodified.

sort arranges the elements of the list from low to high:

<A NAME="@default782"></A> <A NAME="@default783"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> t = ['d', 'c', 'e', 'b', 'a']
>>> t.sort()
>>> print t
['a', 'b', 'c', 'd', 'e']
</FONT></FONT>

List methods are all void; they modify the list and return None.

If you accidentally write t = t.sort(), you will be disappointed

with the result.

<A NAME="@default784"></A>

<A NAME="@default785"></A> <A NAME="@default786"></A>

<A NAME="@default787"></A>

<A NAME="toc111"></A><A NAME="htoc123">10.7</A>  Map, filter and reduce

To add up all the numbers in a list, you can use a loop like this:

<FONT COLOR=blue><FONT SIZE=4>def add_all(t):
    total = 0
    for x in t:
        total += x
    return total
</FONT></FONT>

total is initialized to 0. Each time through the loop,

x gets one element from the list. The += operator

provides a short way to update a variable:

<A NAME="@default788"></A> <A NAME="@default789"></A>

<FONT COLOR=blue><FONT SIZE=4>    total += x
</FONT></FONT>

is equivalent to:

<FONT COLOR=blue><FONT SIZE=4>    total = total + x
</FONT></FONT>

As the loop executes, total accumulates the sum of the

elements; a variable used this way is sometimes called an

accumulator.

<A NAME="@default790"></A>

Adding up the elements of a list is such a common operation that Python provides it as a built-in function, sum:

<FONT COLOR=blue><FONT SIZE=4>>>> t = [1, 2, 3]
>>> sum(t)
6
</FONT></FONT>

An operation like this that combines a sequence of elements into a single value is sometimes called reduce.

<A NAME="@default791"></A>

<A NAME="@default792"></A>

<A NAME="@default793"></A>

Sometimes you want to traverse one list while building

another. For example, the following function takes a list of strings

and returns a new list that contains capitalized strings:

<FONT COLOR=blue><FONT SIZE=4>def capitalize_all(t):
    res = []
    for s in t:
        res.append(s.capitalize())
    return res
</FONT></FONT>

res is initialized with an empty list; each time through

the loop, we append the next element. So res is another

kind of accumulator.

<A NAME="@default794"></A>

An operation like capitalize_all is sometimes called a map because it “maps” a function (in this case the method capitalize) onto each of the elements in a sequence.

<A NAME="@default795"></A>

<A NAME="@default796"></A> <A NAME="@default797"></A>

<A NAME="@default798"></A>

Another common operation is to select some of the elements from

a list and return a sublist. For example, the following function takes a list of strings and returns a list that contains

only the uppercase strings:

<FONT COLOR=blue><FONT SIZE=4>def only_upper(t):
    res = []
    for s in t:
        if s.isupper():
            res.append(s)
    return res
</FONT></FONT>

isupper is a string method that returns True if the string contains only upper case letters.

An operation like only_upper is called a filter because it selects some of the elements and filters out the others.

Most common list operations can be expressed as a combination

of map, filter and reduce. Because these operations are so common, Python provides language features to support them, including the built-in function map and an operator

called a “list comprehension.”

<A NAME="@default799"></A>

Exercise 1  

<A NAME="cumulative"></A>

<A NAME="@default800"></A>

Write a function that takes a list of numbers and returns the cumulative sum; that is, a new list where the ith element is the sum of the first i+1 elements from the original list. For example, the cumulative sum of [1, 2, 3] is [1, 3, 6].

<A NAME="toc112"></A><A NAME="htoc124">10.8</A>  Deleting elements

<A NAME="@default801"></A> <A NAME="@default802"></A>

There are several ways to delete elements from a list. If you

know the index of the element you want, you can use

pop:

<A NAME="@default803"></A> <A NAME="@default804"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> t = ['a', 'b', 'c']
>>> x = t.pop(1)
>>> print t
['a', 'c']
>>> print x
b
</FONT></FONT>

pop modifies the list and returns the element that was removed.

If you don’t provide an index, it deletes and returns the

last element.

If you don’t need the removed value, you can use the del operator:

<A NAME="@default805"></A> <A NAME="@default806"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> t = ['a', 'b', 'c']
>>> del t[1]
>>> print t
['a', 'c']
</FONT></FONT>

If you know the element you want to remove (but not the index), you can use remove:

<A NAME="@default807"></A> <A NAME="@default808"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> t = ['a', 'b', 'c']
>>> t.remove('b')
>>> print t
['a', 'c']
</FONT></FONT>

The return value from remove is None.

<A NAME="@default809"></A> <A NAME="@default810"></A>

To remove more than one element, you can use del with a slice index:

<FONT COLOR=blue><FONT SIZE=4>>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> del t[1:5]
>>> print t
['a', 'f']
</FONT></FONT>

As usual, the slice selects all the elements up to, but not including, the second index.

<A NAME="toc113"></A><A NAME="htoc125">10.9</A>  Lists and strings

<A NAME="@default811"></A>

<A NAME="@default812"></A>

<A NAME="@default813"></A>

A string is a sequence of characters and a list is a sequence

of values, but a list of characters is not the same as a string. To convert from a string to a list of characters,

you can use list:

<A NAME="@default814"></A> <A NAME="@default815"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> s = 'spam'
>>> t = list(s)
>>> print t
['s', 'p', 'a', 'm']
</FONT></FONT>

Because list is the name of a built-in function, you should

avoid using it as a variable name. I also avoid l because

it looks too much like 1. So that’s why I use t.

The list function breaks a string into individual letters. If

you want to break a string into words, you can use the split

method:

<A NAME="@default816"></A> <A NAME="@default817"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> s = 'pining for the fjords'
>>> t = s.split()
>>> print t
['pining', 'for', 'the', 'fjords']
</FONT></FONT>

An optional argument called a delimiter specifies which

characters to use as word boundaries. The following example

uses a hyphen as a delimiter:

<A NAME="@default818"></A>

<A NAME="@default819"></A>

<A NAME="@default820"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> s = 'spam-spam-spam'
>>> delimiter = '-'
>>> s.split(delimiter)
['spam', 'spam', 'spam']
</FONT></FONT>

join is the inverse of split. It

takes a list of strings and concatenates the elements. join is a string method, so you have to invoke it on the delimiter and pass the

list as a parameter:

<A NAME="@default821"></A>

<A NAME="@default822"></A>

<A NAME="@default823"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> t = ['pining', 'for', 'the', 'fjords']
>>> delimiter = ' '
>>> delimiter.join(t)
'pining for the fjords'
</FONT></FONT>

In this case the delimiter is a space character, so

join puts a space between words. To concatenate strings without spaces, you can use the empty string,

, as a delimiter.

<A NAME="@default824"></A> <A NAME="@default825"></A>

<A NAME="toc114"></A><A NAME="htoc126">10.10</A>  Objects and values

<A NAME="@default826"></A> <A NAME="@default827"></A>

If we execute these assignment statements:

<FONT COLOR=blue><FONT SIZE=4>a = 'banana'
b = 'banana'
</FONT></FONT>

We know that a and b both refer to a

string, but we don’t know whether they refer to the same string.

There are two possible states:

<A NAME="@default828"></A>

<IMG SRC="book014.png">

In one case, a and b refer to two different objects that

have the same value. In the second case, they refer to the same

object.

<A NAME="@default829"></A> <A NAME="@default830"></A>

To check whether two variables refer to the same object, you can use the is operator.

<FONT COLOR=blue><FONT SIZE=4>>>> a = 'banana'
>>> b = 'banana'
>>> a is b
True
</FONT></FONT>

In this example, Python only created one string object, and both a and b refer to it.

But when you create two lists, you get two objects:

<FONT COLOR=blue><FONT SIZE=4>>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a is b
False
</FONT></FONT>

So the state diagram looks like this:

<A NAME="@default831"></A> <A NAME="@default832"></A>

<IMG SRC="book015.png">

In this case we would say that the two lists are equivalent,

because they have the same elements, but not identical, because they are not the same object. If two objects are identical, they are also equivalent, but if they are equivalent, they are not necessarily

identical.

<A NAME="@default833"></A> <A NAME="@default834"></A>

Until now, we have been using “object” and “value”

interchangeably, but it is more precise to say that an object has a value. If you execute a = [1,2,3], a refers to a list object whose value is a particular sequence of elements. If another

list has the same elements, we would say it has the same value.

<A NAME="@default835"></A> <A NAME="@default836"></A>

<A NAME="toc115"></A><A NAME="htoc127">10.11</A>  Aliasing

<A NAME="@default837"></A> <A NAME="@default838"></A>

If a refers to an object and you assign b = a, then both variables refer to the same object:

<FONT COLOR=blue><FONT SIZE=4>>>> a = [1, 2, 3]
>>> b = a
>>> b is a
True
</FONT></FONT>

The state diagram looks like this:

<A NAME="@default839"></A> <A NAME="@default840"></A>

<IMG SRC="book016.png">

The association of a variable with an object is called a reference. In this example, there are two references to the same object.

<A NAME="@default841"></A>

An object with more than one reference has more than one name, so we say that the object is aliased.

<A NAME="@default842"></A>

If the aliased object is mutable,

changes made with one alias affect

the other:

<FONT COLOR=blue><FONT SIZE=4>>>> b[0] = 17
>>> print a
[17, 2, 3]
</FONT></FONT>

Although this behavior can be useful, it is error-prone. In general,

it is safer to avoid aliasing when you are working with mutable

objects.

<A NAME="@default843"></A>

For immutable objects like strings, aliasing is not as much of a problem. In this example:

<FONT COLOR=blue><FONT SIZE=4>a = 'banana'
b = 'banana'
</FONT></FONT>

It almost never makes a difference whether a and b refer to the same string or not.

<A NAME="toc116"></A><A NAME="htoc128">10.12</A>  List arguments

<A NAME="@default844"></A>

<A NAME="@default845"></A> <A NAME="@default846"></A> <A NAME="@default847"></A>

<A NAME="@default848"></A>

When you pass a list to a function, the function gets a reference

to the list. If the function modifies a list parameter, the caller sees the change.

For example, delete_head removes the first element from a list:

<FONT COLOR=blue><FONT SIZE=4>def delete_head(t):
    del t[0]
</FONT></FONT>

Here’s how it is used:

<FONT COLOR=blue><FONT SIZE=4>>>> letters = ['a', 'b', 'c']

>>> delete_head(letters) >>> print letters ['b', 'c']

</FONT></FONT>

The parameter t and the variable letters are

aliases for the same object. The stack diagram looks like

this:

<A NAME="@default849"></A> <A NAME="@default850"></A>

<IMG SRC="book017.png">

Since the list is shared by two frames, I drew it between them.

It is important to distinguish between operations that

modify lists and operations that create new lists. For example, the append method modifies a list, but the

+ operator creates a new list:

<A NAME="@default851"></A>

<A NAME="@default852"></A> <A NAME="@default853"></A>

<A NAME="@default854"></A>

<FONT COLOR=blue><FONT SIZE=4>>>> t1 = [1, 2]
>>> t2 = t1.append(3)
>>> print t1
[1, 2, 3]
>>> print t2
None

>>> t3 = t1 + [3]
>>> print t3
[1, 2, 3]
>>> t2 is t3
False
</FONT></FONT>

This difference is important when you write functions that

are supposed to modify lists. For example, this function

does not delete the head of a list:

<FONT COLOR=blue><FONT SIZE=4>def bad_delete_head(t):
    t = t[1:]              # WRONG!
</FONT></FONT>

The slice operator creates a new list and the assignment

makes t refer to it, but none of that has any effect

on the list that was passed as an argument.

<A NAME="@default855"></A> <A NAME="@default856"></A>

An alternative is to write a function that creates and

returns a new list. For example, tail returns all but the first

element of a list:

<FONT COLOR=blue><FONT SIZE=4>def tail(t):
    return t[1:]
</FONT></FONT>

This function leaves the original list unmodified. Here’s how it is used:

<FONT COLOR=blue><FONT SIZE=4>>>> letters = ['a', 'b', 'c']
>>> rest = tail(letters)
>>> print rest
['b', 'c']
</FONT></FONT>
Exercise 2  

Write a function called chop that takes a list and modifies it, removing the first and last elements, and returns None.

Then write a function called middle that takes a list and

returns a new list that contains all but the first and last

elements.

<A NAME="toc117"></A><A NAME="htoc129">10.13</A>  Debugging

<A NAME="@default857"></A>

Careless use of lists (and other mutable objects)

can lead to long hours of debugging. Here are some common

pitfalls and ways to avoid them:

  1. Don’t forget that most list methods modify the argument and

    return None. This is the opposite of the string methods,

    which return a new string and leave the original alone.

    If you are used to writing string code like this:

    <FONT COLOR=blue><FONT SIZE=4>word = word.strip()
    </FONT></FONT>

    It is tempting to write list code like this:

    <FONT COLOR=blue><FONT SIZE=4>t = t.sort()           # WRONG!
    </FONT></FONT>

    <A NAME="@default858"></A> <A NAME="@default859"></A>

    Because sort returns None, the next operation you perform with t is likely to fail.

    Before using list methods and operators, you should read the

    documentation carefully and then test them in interactive mode. The methods and operators that lists share with other sequences (like strings) are documented at docs.python.org/lib/typesseq.html. The methods and operators that only apply to mutable sequences

    are documented at docs.python.org/lib/typesseq-mutable.html.

  2. Pick an idiom and stick with it.

    Part of the problem with lists is that there are too many

    ways to do things. For example, to remove an element from a list, you can use pop, remove, del,

    or even a slice assignment.

    To add an element, you can use the append method or the + operator. But don’t forget that these are right:

    <FONT COLOR=blue><FONT SIZE=4>t.append(x)
    t = t + [x]
    </FONT></FONT>

    And these are wrong:

    <FONT COLOR=blue><FONT SIZE=4>t.append([x])          # WRONG!
    

    t = t.append(x) # WRONG! t + [x] # WRONG! t = t + x # WRONG!

    </FONT></FONT>

    Try out each of these examples in interactive mode to make sure

    you understand what they do. Notice that only the last one causes a runtime error; the other three are legal, but they

    do the wrong thing.

  3. Make copies to avoid aliasing.

    <A NAME="@default860"></A> <A NAME="@default861"></A>

    If you want to use a method like sort that modifies

    the argument, but you need to keep the original list as

    well, you can make a copy.

    <FONT COLOR=blue><FONT SIZE=4>orig = t[:]
    t.sort()
    </FONT></FONT>

    In this example you could also use the built-in function sorted,

    which returns a new, sorted list and leaves the original alone. But in that case you should avoid using sorted as a variable

    name!

<A NAME="toc118"></A><A NAME="htoc130">10.14</A>  Glossary

list:
A sequence of values. <A NAME="@default862"></A>
element:
One of the values in a list (or other sequence), also called items. <A NAME="@default863"></A>
index:
An integer value that indicates an element in a list. <A NAME="@default864"></A>
nested list:
A list that is an element of another list. <A NAME="@default865"></A>
list traversal:
The sequential accessing of each element in a list. <A NAME="@default866"></A>
mapping:
A relationship in which each element of one set corresponds to an element of another set. For example, a list is a mapping from indices to elements. <A NAME="@default867"></A>
accumulator:
A variable used in a loop to add up or accumulate a result. <A NAME="@default868"></A>

<A NAME="@default869"></A>

reduce:
A processing pattern that traverses a sequence

and accumulates the elements into a single result. <A NAME="@default870"></A>

<A NAME="@default871"></A>
map:
A processing pattern that traverses a sequence and performs an operation on each element. <A NAME="@default872"></A> <A NAME="@default873"></A>
filter:
A processing pattern that traverses a list and selects the elements that satisfy some criterion. <A NAME="@default874"></A> <A NAME="@default875"></A>
object:
Something a variable can refer to. An object has a type and a value. <A NAME="@default876"></A>
equivalent:
Having the same value. <A NAME="@default877"></A>
identical:
Being the same object (which implies equivalence). <A NAME="@default878"></A>
reference:
The association between a variable and its value. <A NAME="@default879"></A>
aliasing:
A circumstance where two variables refer to the same object. <A NAME="@default880"></A>
delimiter:
A character or string used to indicate where a string should be split. <A NAME="@default881"></A>

<A NAME="toc119"></A><A NAME="htoc131">10.15</A>  Exercises

Exercise 3  

Write a function called is_sorted that takes a list as a parameter and returns True if the list is sorted in ascending order and False otherwise. You can assume (as a precondition) that the elements of the list can be compared with the comparison

operators <, >, etc.

<A NAME="@default882"></A>

For example, is_sorted([1,2,2]) should return True

and is_sorted(['b','a']) should return False.

Exercise 4   <A NAME="anagram"></A>

<A NAME="@default883"></A>

Two words are anagrams if you can rearrange the letters from one

to spell the other. Write a function called is_anagram that takes two strings and returns True if they are anagrams.

Exercise 5   <A NAME="duplicate"></A>

The (so-called) Birthday Paradox:

  1. <A NAME="@default884"></A> <A NAME="@default885"></A>

    Write a function called has_duplicates that takes

    a list and returns True if there is any element that appears more than once. It should not modify the original

    list.
  2. If there are 23 students in your class, what are the chances that two of you have the same birthday? You can estimate this probability by generating random samples of 23 birthdays and checking for matches. Hint: you can generate random birthdays with the randint function in the random module.

    <A NAME="@default886"></A> <A NAME="@default887"></A> <A NAME="@default888"></A> <A NAME="@default889"></A>

You can read about this problem at

wikipedia.org/wiki/Birthday_paradox, and you can see my solution

at thinkpython.com/code/birthday.py.

Exercise 6  

<A NAME="@default890"></A> <A NAME="@default891"></A>

Write a function called remove_duplicates that takes

a list and returns a new list with only the unique elements from the original. Hint: they don’t have to be in the same order.

Exercise 7  

<A NAME="@default892"></A> <A NAME="@default893"></A> <A NAME="@default894"></A>

<A NAME="@default895"></A>

Write a function that reads the file words.txt and builds a list with one element per word. Write two versions of this function, one using the append method and the other using the idiom t = t + [x]. Which one takes longer to run? Why?

You can see my solution at thinkpython.com/code/wordlist.py.

Exercise 8  

<A NAME="wordlist1"></A>

<A NAME="bisection"></A>

<A NAME="@default896"></A> <A NAME="@default897"></A> <A NAME="@default898"></A>

To check whether a word is in the word list, you could use

the in operator, but it would be slow because it searches

through the words in order.

Because the words are in alphabetical order, we can speed things up

with a bisection search, which is similar to what you do when you look a word up in the dictionary. You start in the middle and check to see whether the word you are looking for comes before the word in the middle of the list. If so, then you search the first half of the list

the same way. Otherwise you search the second half.

Either way, you cut the remaining search space in half. If the

word list has 113,809 words, it will take about 17 steps to

find the word or conclude that it’s not there.

Write a function called bisect that takes a sorted list

and a target value and returns the index of the value

in the list, if it’s there, or None if it’s not.

<A NAME="@default899"></A> <A NAME="@default900"></A>

Or you could read the documentation of the bisect module

and use that!

Exercise 9   <A NAME="@default901"></A>

Two words are a “reverse pair” if each is the reverse of the other. Write a program that finds all the reverse pairs in the word list.

Exercise 10   <A NAME="@default902"></A>

Two words “interlock” if taking alternating letters from each forms a new word<A NAME="text20" HREF="#note20">1</A>. For example, “shoe” and “cold” interlock to form “schooled.”

  1. Write a program that finds all pairs of words that interlock. Hint: don’t enumerate all pairs!
  2. Can you find any words that are three-way interlocked; that is, every third letter forms a word, starting from the first, second or third?

<A NAME="note20" HREF="#text20">1</A>
This exercise is inspired by an example at puzzlers.org.

<A HREF="book010.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A> <A HREF="index.html"><IMG SRC="contents_motif.gif" ALT="Up"></A> <A HREF="book012.html"><IMG SRC="next_motif.gif" ALT="Next"></A> </BODY> </HTML>