List Methods

Lists are objects and thus have methods. Some of the most commonly used are listed in Figure 5.9. Here is a sample interaction showing how we can use these methods to construct a list containing all the colors of the rainbow:

Download lists/colors.cmd

>>> colors = 'red orange green black blue'.split() >>> colors.append( 'purple') >>> colors

['red', 'orange', 'green', 'black', 'blue', 'purple']

Method

L.remove(v)

L.reverse()

Where Did My List Go?

Beginning programmers often forget that many list methods return None rather than creating and returning a new list. (Experienced programmers sometimes forget too.) As a result, their lists sometimes seem to disappear:

Download lists/colors2.cmd

>>> colors = 'red orange yellow green blue purple'.split() >>> colors

['blue', 'green', 'orange', 'purple', 'red', 'yellow'] >>> sorted_colors = colors.sort() >>> print sorted_colors None

As we'll discuss in Section 4.5, Testing, on page 70, mistakes like these can quickly be caught by writing and running a few tests.

>>> colors.insert(2, 'yellow') >>> colors

['red', 'orange', 'yellow', 'green', >>> colors.remove( 'black') >>> colors

'black' , 'blue' , 'black' , 'purple' ] 'blue' , 'purple' ]

It is important to note that all these methods modify the list instead of creating a new list. They do this because lists can grow very, very largeā€”a million patient records, for example, or a billion measurements of a magnetic field. Creating a new list every time someone wanted to make a change to such a list would slow Python down so much that it would no longer be useful; having Python guess when it should make a copy, and when it should operate on the list in place, would make it impossible to figure out.

It's just as important to remember that all of these methods except pop return the special value None, which means "There is no useful information" or "There's nothing here." Python doesn't display anything when asked to display the value None. Printing it, on the other hand, shows us that it's there:

Download lists/none.cmd

0 12

0 12

Figure 5.10: Nested lists

Finally, a call to append is not the same as using +. First, append appends a single value, while + expects two lists as operands. Second, append modifies the list rather than creating a new one.

5.8 Nested Lists

We said in Section 5.1, Lists Are Heterogeneous, on page 85 that lists can contain any type of data. That means that they can contain other lists, just as the body of a loop can contain another loop. For example, the following nested list describes life expectancies in different countries:

Download lists/lifelist.py

[['Canada', 76.5], ['United States' , 75.5], ['Mexico', 72.0]]

As shown in Figure 5.10, each element of the outer list is itself a list of two items. We use the standard notation to access the items in the outer list:

Download lists/lifeO.cmd

>>> life = [['Canada', 76.5], ['United States' , 75.5], ['Mexico', 72.0]] >>> life[0] ['Canada', 76.5] >>> life[1]

['United States', 75.5] >>> life[2] ['Mexico', 72.0]

Figure 5.11: Aliasing sublists

Since each of these items is also a list, we can immediately index it again, just as we can chain together method calls or pass the result of one function call as an argument to another function:

Download lists/lifel.cmd

>>> life = [['Canada', 76.5], ['United States' , 75.5], ['Mexico', 72.0]] >>> life[1]

['United States', 75.5] >>> life[1][0] 'United States' >>> life[1][1] 75.5

We can also assign sublists to variables:

Download lists/life2.cmd

>>> life = [['Canada', 76.5], ['United States' , 75.5], ['Mexico', 72.0]] >>> canada = life[0] >>> canada ['Canada', 76.5] >>> canada[0] 'Canada' >>> canada[1] 76.5

Assigning a sublist to a variable creates an alias for that sublist (see Figure 5.11). As before, any change we make through the sublist refer -ence will show up when we access the main list, and vice versa:

Download lists/life3.cmd

>>> life = [['Canada', 76.5], ['United States' , 75.5], ['Mexico', 72.0]] >>> canada = life[0]

>>> canada[1] = 80.0 >>> canada ['Canada', 80.0] >>> life

[['Canada', 80.0], ['United States' , 75.5], ['Mexico', 72.0]]

Was this article helpful?

0 0

Post a comment