Marquette University, view of Wisconsin Avenue  

Module 11

Lists

Python has a very rich set of data structures. The simplest is the list. A list is an ordered collection of elements. A list can mix any number of objects of different types, including lists themselves.

We can define lists by listing a number of objects between square brackets, as for example in lista = ['a', 2, 1.2, [0, 1]], which defines a list consisting of a string, an integer, a floating point number, and a list. Elements in a list are accessed using the bracket operation, which takes the name of a list, then a bracket, then an index, and then again a bracket. Indices start with zero and negative indices count from the end of the list.

There is a large number of operations on lists. The most important operation is the append method. Its syntax is list_name.append(object). This will add the element object at the end of the list called list_name.

For loops in Python use a list like container. Thus for x in [3,5,7,11,13]: will repeat the following block with x taking the values 3, 5, 6, 11, and 13 successively.