CEV 208 Computer Programming Chapter 10 Lists Spring

  • Slides: 25
Download presentation
CEV 208 Computer Programming Chapter 10 Lists Spring 2013

CEV 208 Computer Programming Chapter 10 Lists Spring 2013

A list is a sequence • The values in a list can be of

A list is a sequence • The values in a list can be of any type. • The values in a list are called elements or items. A list within another list is nested. You can assign list values to variables.

Lists are mutable Each index maps to one of the elements.

Lists are mutable Each index maps to one of the elements.

List indices work the same way as string indices: • Any integer expression can

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 Index. Error. • If an index has a negative value, it counts backward from the end of the list.

The in operator

The in operator

Traversing a list What is the length of this list? :

Traversing a list What is the length of this list? :

List operations The + operator: The * operator:

List operations The + operator: The * operator:

List slices A simple way to make a copy of a list: You can

List slices A simple way to make a copy of a list: You can update multiple elements by a LHS slice opearator:

List methods The append method: The extend method: The sort method: These list methods

List methods The append method: The extend method: The sort method: These list methods are all void!

reduce augmented assignment statement accumulator

reduce augmented assignment statement accumulator

map

map

filter

filter

Assignment

Assignment

Deleting elements The pop method modifies the list and returns the element that was

Deleting elements The pop method modifies the list and returns the element that was removed. If you don’t need the removed value, you can use the del operator.

Deleting elements If you know the element to remove but not its index, you

Deleting elements If you know the element to remove but not its index, you can use the remove method. remove returns None. To remove more than one element:

Lists and strings The list function: The split method has an optional argument called

Lists and strings The list function: The split method has an optional argument called a delimiter.

Lists and strings The string method join is the inverse of split:

Lists and strings The string method join is the inverse of split:

Objects and values There are two possible states: Use the is operator to see

Objects and values There are two possible states: Use the is operator to see which state is correct. These two lists are equivalent, but they are not identical.

Aliasing In this example, there are two references to the same object. In this

Aliasing In this example, there are two references to the same object. In this example, the object [1, 2, 3] is aliased. It has more than one name. Since lists are mutable:

List arguments When you pass a list to a function, the function gets a

List arguments When you pass a list to a function, the function gets a reference to the list.

List arguments It is important to distinguish between operations that modify lists and that

List arguments It is important to distinguish between operations that modify lists and that create new lists

List arguments

List arguments

Assignment

Assignment

Debugging • Don’t forget that most list methods modify the argument and return None.

Debugging • 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. • Part of the problem with lists is that there are too many ways to do things. • Make copies to avoid aliasing.

Glossary list accumulator equivalent element augmented assignment identical index reduce reference nested list map

Glossary list accumulator equivalent element augmented assignment identical index reduce reference nested list map aliasing list traversal filter delimiter mapping object