Section 6.11. Lists


6.11. Lists

Like strings, lists provide sequential storage through an index offset and access to single or consecutive elements through slices. However, the comparisons usually end there. Strings consist only of characters and are immutable (cannot change individual elements), while lists are flexible container objects that hold an arbitrary number of Python objects. Creating lists is simple; adding to lists is easy, too, as we see in the following examples.

The objects that you can place in a list can include standard types and objects as well as user-defined ones. Lists can contain different types of objects and are more flexible than an array of C structs or Python arrays (available through the external array module) because arrays are restricted to containing objects of a single type. Lists can be populated, empty, sorted, and reversed. Lists can be grown and shrunk. They can be taken apart and put together with other lists. Individual or multiple items can be inserted, updated, or removed at will.

Tuples share many of the same characteristics of lists and although we have a separate section on tuples, many of the examples and list functions are applicable to tuples as well. The key difference is that tuples are immutable, i.e., read-only, so any operators or functions that allow updating lists, such as using the slice operator on the left-hand side of an assignment, will not be valid for tuples.

How to Create and Assign Lists

Creating lists is as simple as assigning a value to a variable. You handcraft a list (empty or with elements) and perform the assignment. Lists are delimited by surrounding square brackets ( [ ] ). You can also use the factory function.

 >>> aList = [123, 'abc', 4.56, ['inner', 'list'], 7-9j] >>> anotherList = [None, 'something to see here'] >>> print aList [123, 'abc', 4.56, ['inner', 'list'], (7-9j)] >>> print anotherList [None, 'something to see here'] >>> aListThatStartedEmpty = [] >>> print aListThatStartedEmpty [] >>> list('foo') ['f', 'o', 'o']


How to Access Values in Lists

Slicing works similar to strings; use the square bracket slice operator ([ ] ) along with the index or indices.

>>> aList[0] 123 >>> aList[1:4] ['abc', 4.56, ['inner', 'list']] >>> aList[:3] [123, 'abc', 4.56] >>> aList[3][1] 'list'


How to Update Lists

You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method:

>>> aList [123, 'abc', 4.56, ['inner', 'list'], (7-9j)] >>> aList[2] 4.56 >>> aList[2] = 'float replacer' >>> aList [123, 'abc', 'float replacer', ['inner', 'list'], (7-9j)] >>> >>> anotherList.append("hi, i'm new here") >>> print anotherList [None, 'something to see here', "hi, i'm new here"] >>> aListThatStartedEmpty.append('not empty anymore') >>> print aListThatStartedEmpty ['not empty anymore']


How to Remove List Elements and Lists

To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know.

>>> aList [123, 'abc', 'float replacer', ['inner', 'list'], (7-9j)] >>> del aList[1] >>> aList [123, 'float replacer', ['inner', 'list'], (7-9j)] >>> aList.remove(123) >>> aList ['float replacer', ['inner', 'list'], (7-9j)]


You can also use the pop() method to remove and return a specific object from a list.

Normally, removing an entire list is not something application programmers do. Rather, they tend to let it go out of scope (i.e., program termination, function call completion, etc.) and be deallocated, but if they do want to explicitly remove an entire list, they use the del statement:

del aList




Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net