next up previous contents
Next: List Indexing and Slicing Up: Lists, Tuples and Dictionaries Previous: Lists, Tuples and Dictionaries   Contents

List Data

Lists provide a general mechanism for storing a collection of objects indexed by a number in python. The elements of the list are arbitrary -- they can be numbers, strings, functions, user-defined objects or even other lists, making complex data structures very simple to express in python. You can input a list to the python interpreter by surrounding a comma separated list of the objects you want in the list with square brackets ([ ]) Thus, a simple list of numbers can be created as follows:
>>> mylist = [1,7,9, 13, 22, 31]
Python ignores spaces between the entries of a list. If you need to span multiple lines with a list entry, you can simply hit the return key after any comma in the list:
>>> newlist = [7, 9, 12, 15,
... 17,19,103]
Note that the python interpreter changes its prompt when it recognizes a continuation line, and that no indentation is necessary to continue a line like this. Inside a script, your input can also be broken up after commas in a similar fashion. To create an empty list, use square brackets with no elements in between them ([]).

The elements of a list need not be of the same type. The following list contains numeric, string and list data, along with a function:

>>> mixlist = [7,'dog','tree',[1,5,2,7],abs]
Since the token abs was entered in the list without quotes, python interprets it as a named object; in this case it represents the builtin function abs. Since functions and many other objects can't be displayed in the same fashion as a number or a string, python uses a special notation to display them:
>>> mixlist
[7, 'dog', 'tree', [1, 5, 2, 7], <built-in function abs>]
The angle brackets surrounding the phrase ``built-in function abs'' indicate that that element of the list is a special object of some sort.

To access the individual elements of a list, use square brackets after the list's name surrounding the index of the desired element. Recall that the first element of a sequence in python is numbered zero. Thus, to extract the abs function from mylist in the example above, we would refer to element 4, i.e. mylist[4]:

>>> mixlist[4](-5)
This example shows that once an element is extracted from a list, you can use it in any context in which the original element could be used; by providing an argument in parentheses to mixlist[4], we call the function abs which was stored in that position in the list. Furthermore, when accessing lists inside of lists, you can simply use additional sets of square brackets to access individual elements:
>>> nestlist = [1,2,[10,20,30,[7,9,11,[100,200,300]]],[1,7,8]]
>>> nestlist[2]
[10, 20, 30, [7, 9, 11, [100, 200, 300]]]
>>> nestlist[2][3]
[7, 9, 11, [100, 200, 300]]
>>> nestlist[2][3][3]
[100, 200, 300]
>>> nestlist[2][3][3][0]
While this is a competely artificial example, it shows that you can nest lists to any level you choose, and that the individual elements of those lists are always extracted in a simple, consistent way.

You may notice a similarity between the way you access elements in a list, and the way you access individual characters in a character string (Section 2.4.3). This is because both strings and lists are examples of python sequences, and they behave consistently. For example, to find out the number of elements in a list, you can use the built-in function len, just as you would to find the number of characters in a character string. Calling len with a non-sequence argument results in a TypeError exception, however.

>>> len(mixlist)
>>> len(mixlist[3])
>>> len(mixlist[1])
>>> len(mixlist[0])
Traceback (innermost last):
  File "<stdin>", line 1, in ?
TypeError: len() of unsized object
In the first case, calling len with the argument mixlist returns the number of elements in the list, namely 5. Similarly, refering to mixlist[3], corresponding to the list [[1, 5, 2, 7] returns 4, the number of elements in that list. Calling len with mixlist[1] (which is the string ``dog'') returns the number of characters in the string, but calling len with the scalar argument mixlist[0] (which is the integer 7), results in an exception.

To convert a string into a list, making each character in the string a separate element in the resulting list, use the list function.

next up previous contents
Next: List Indexing and Slicing Up: Lists, Tuples and Dictionaries Previous: Lists, Tuples and Dictionaries   Contents
Phil Spector 2003-11-12