Python Lists: An Ordered Collection of Values, Study notes of Electrical and Electronics Engineering

This chapter explains how to create, access, and manipulate lists in python. Lists are ordered collections of values, where each value is identified by an index. They can contain elements of different types and can be nested. Creating lists, accessing elements, determining list length, updating and deleting elements, and using lists with for loops.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-szd
koofers-user-szd 🇺🇸

9 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 8
Lists
Alist is an ordered set of values, where each value is identified by an index.
The values that make up a list are called its elements. Lists are similar to
strings, which are ordered sets of characters, except that the elements of a list
can have any type. Lists and strings—and other things that behave like ordered
sets—are called sequences.
8.1 List values
There are several ways to create a new list; the simplest is to enclose the elements
in square brackets ([and ]):
[10, 20, 30, 40]
["spam", "bungee", "swallow"]
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 (mirabile dictu) another list:
["hello", 2.0, 5, [10, 20]]
A list within another list is said to be nested.
Lists that contain consecutive integers are common, so Python provides a simple
way to create them:
>>> range(1,5)
[1, 2, 3, 4]
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Python Lists: An Ordered Collection of Values and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Chapter 8

Lists

A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can have any type. Lists and strings—and other things that behave like ordered sets—are called sequences.

8.1 List values

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

[10, 20, 30, 40] ["spam", "bungee", "swallow"]

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 (mirabile dictu) another list:

["hello", 2.0, 5, [10, 20]]

A list within another list is said to be nested.

Lists that contain consecutive integers are common, so Python provides a simple way to create them:

range(1,5) [1, 2, 3, 4]

82 Lists

The range function takes two arguments and returns a list that contains all the integers from the first to the second, including the first but not including the second!

There are two other forms of range. With a single argument, it creates a list that starts at 0:

range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

If there is a third argument, it specifies the space between successive values, which is called the step size. This example counts from 1 to 10 by steps of 2:

range(1, 10, 2) [1, 3, 5, 7, 9]

Finally, there is a special list that contains no elements. It is called the empty list, and it is denoted [].

With all these ways to create lists, it would be disappointing if we couldn’t assign list values to variables or pass lists as parameters to functions. We can.

vocabulary = ["ameliorate", "castigate", "defenestrate"] numbers = [17, 123] empty = [] print vocabulary, numbers, empty [’ameliorate’, ’castigate’, ’defenestrate’] [17, 123] []

8.2 Accessing elements

The syntax for accessing the elements of a list is the same as the syntax 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:

print numbers[0] numbers[1] = 5

The bracket operator can appear anywhere in an expression. When it appears on the left side of an assignment, it changes one of the elements in the list, so the one-eth element of numbers, which used to be 123, is now 5.

Any integer expression can be used as an index:

numbers[3-2] 5 numbers[1.0] TypeError: sequence index must be integer

84 Lists

horsemen = ["war", "famine", "pestilence", "death"]

i = 0 while i < len(horsemen): print horsemen[i] i = i + 1

The last time the body of the loop is executed, i is len(horsemen) - 1, which is the index of the last element. When i is equal to len(horsemen), the condition fails and the body is not executed, which is a good thing, because len(horsemen) is not a legal index.

Although a list can contain another list, the nested list still counts as a single element. The length of this list is four:

[’spam!’, 1, [’Brie’, ’Roquefort’, ’Pol le Veq’], [1, 2, 3]]

As an exercise, write a loop that traverses the previous list and prints the length of each element. What happens if you send an integer to len?

8.4 List membership

in is a boolean operator that tests membership in a sequence. We used it in Section 7.10 with strings, but it also works with lists and other sequences:

horsemen = [’war’, ’famine’, ’pestilence’, ’death’] ’pestilence’ in horsemen 1 ’debauchery’ in horsemen 0

Since “pestilence” is a member of the horsemen list, the in operator returns true. Since “debauchery” is not in the list, in returns false.

We can use the not in combination with in to test whether an element is not a member of a list:

’debauchery’ not in horsemen 1

8.5 Lists and for loops

The for loop we saw in Section 7.3 also works with lists. The generalized syntax of a for loop is:

8.6 List operations 85

for VARIABLE in LIST: BODY

This statement is equivalent to:

i = 0 while i < len(LIST): VARIABLE = LIST[i] BODY i = i + 1

The for loop is more concise because we can eliminate the loop variable, i. Here is the previous loop written with a for loop.

for horseman in horsemen: print horseman

It almost reads like English: “For (every) horseman in (the list of) horsemen, print (the name of the) horseman.”

Any list expression can be used in a for loop:

for number in range(20): if number % 2 == 0: print number

for fruit in ["banana", "apple", "quince"]: print "I like to eat " + fruit + "s!"

The first example prints all the even numbers between zero and nineteen. The second example expresses enthusiasm for various fruits.

8.6 List operations

The + operator concatenates lists:

a = [1, 2, 3] b = [4, 5, 6] c = a + b print c [1, 2, 3, 4, 5, 6]

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

8.9 List deletion 87

list = [’a’, ’b’, ’c’, ’d’, ’e’, ’f’] list[1:3] = [] print list [’a’, ’d’, ’e’, ’f’]

And we can add elements to a list by squeezing them into an empty slice at the desired location:

list = [’a’, ’d’, ’f’] list[1:1] = [’b’, ’c’] print list [’a’, ’b’, ’c’, ’d’, ’f’] list[4:4] = [’e’] print list [’a’, ’b’, ’c’, ’d’, ’e’, ’f’]

8.9 List deletion

Using slices to delete list elements can be awkward, and therefore error-prone. Python provides an alternative that is more readable.

del removes an element from a list:

a = [’one’, ’two’, ’three’] del a[1] a [’one’, ’three’]

As you might expect, del handles negative indices and causes a runtime error if the index is out of range.

You can use a slice as an index for del:

list = [’a’, ’b’, ’c’, ’d’, ’e’, ’f’] del list[1:5] print list [’a’, ’f’]

As usual, slices select all the elements up to, but not including, the second index.

88 Lists

8.10 Objects and values

If we execute these assignment statements,

a = "banana" b = "banana"

we know that a and b will refer to a string with the letters "banana". But we can’t tell whether they point to the same string.

There are two possible states:

a b

"banana" "banana"

a b

"banana"

In one case, a and b refer to two different things that have the same value. In the second case, they refer to the same thing. These “things” have names—they are called objects. An object is something a variable can refer to.

Every object has a unique identifier, which we can obtain with the id function. By printing the identifier of a and b, we can tell whether they refer to the same object.

id(a) 135044008 id(b) 135044008

In fact, we get the same identifier twice, which means that Python only created one string, and both a and b refer to it.

Interestingly, lists behave differently. When we create two lists, we get two objects:

a = [1, 2, 3] b = [1, 2, 3] id(a) 135045528 id(b) 135041704

So the state diagram looks like this:

a b

[ 1, 2, 3 ]

[ 1, 2, 3 ]

a and b have the same value but do not refer to the same object.

90 Lists

b[0] = 5 print a [1, 2, 3]

As an exercise, draw a state diagram for a and b before and after this change.

8.13 List parameters

Passing a list as an argument actually passes a reference to the list, not a copy of the list. For example, the function head takes a list as a parameter and returns the first element:

def head(list): return list[0]

Here’s how it is used:

numbers = [1, 2, 3] head(numbers) 1

The parameter list and the variable numbers are aliases for the same object. The state diagram looks like this:

list

[ 1, 2, 3 ]

main numbers

head

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

If a function modifies a list parameter, the caller sees the change. For example, deleteHead removes the first element from a list:

def deleteHead(list): del list[0]

Here’s how deleteHead is used:

numbers = [1, 2, 3] deleteHead(numbers) print numbers [2, 3]

8.14 Nested lists 91

If a function returns a list, it returns a reference to the list. For example, tail returns a list that contains all but the first element of the given list:

def tail(list): return list[1:]

Here’s how tail is used:

numbers = [1, 2, 3] rest = tail(numbers) print rest [2, 3]

Because the return value was created with the slice operator, it is a new list. Creating rest, and any subsequent changes to rest, have no effect on numbers.

8.14 Nested lists

A nested list is a list that appears as an element in another list. In this list, the three-eth element is a nested list:

list = ["hello", 2.0, 5, [10, 20]]

If we print list[3], we get [10, 20]. To extract an element from the nested list, we can proceed in two steps:

elt = list[3] elt[0] 10

Or we can combine them:

list[3][1] 20

Bracket operators evaluate from left to right, so this expression gets the three- eth element of list and extracts the one-eth element from it.

8.15 Matrixes

Nested lists are often used to represent matrices. For example, the matrix:

1 2 3

8.17 Glossary 93

Like split, join takes an optional delimiter that is inserted between elements:

string.join(list, ’_’) ’The_rain_in_Spain...’

As an exercise, describe the relationship between string.join(string.split(song)) and song. Are they the same for all strings? When would they be different?

8.17 Glossary

list: A named collection of objects, where each object is identified by an index.

index: An integer variable or value that indicates an element of a list.

element: One of the values in a list (or other sequence). The bracket operator selects elements of a list.

sequence: Any of the data types that consist of an ordered set of elements, with each element identified by an index.

nested list: A list that is an element of another list.

list traversal: The sequential accessing of each element in a list.

object: A thing to which a variable can refer.

aliases: Multiple variables that contain references to the same object.

clone: To create a new object that has the same value as an existing object. Copying a reference to an object creates an alias but doesn’t clone the object.

delimiter: A character or string used to indicate where a string should be split.