Lists

A list is an ordered collection of objects, or entries. We can add and remove entries from a list, and change a particular entry.

Example:
>>> favs = ['Charlie', 3, 'lemon meringue pie', math.pi]
>>> print favs
['Charlie', 3, 'lemon meringue pie', 3.14159265897931]

Many programming languages have a data structure called an array. An array is an ordered collection of objects which are all of the same type (all integers, all strings, etc.), and the size of an array is chosen when the array is created and the size cannot be changed.

In Python, we have lists instead of arrays. Lists are different from arrays in some ways:
• The type of list entries do not all have to be the same
• The size of a list can increase and decrease

Creating and Modifying Lists

1. Create a list by enumerating the objects in the list

>>> numbers = [2, 3, 5]

2. Append to a list - add an item to the end of the list

>>> myList = []  # create an empty list
>>> myList.append(7)   # myList = [7]
>>> myList.append(11)  # myList = [7, 11]

3. Insert an item at a specified position

>>>  myList = [9, 2, 1]
>>> myList.insert(1, 3)  # myList = [9, 3, 2, 1]

4. Concatenate existing lists

>>> a = [1, 2]
>>> b = [3, 4]
>>> c = a + b      # c = [1, 2, 3, 4]

5. Specify an initial value or values in the list and repeat with *

>>> a = [0, 1]*5
>>> a
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]

6. Create a 2-dimensional list

>>> l2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

The first row contains 1, 2 and 3, the second row contains 4, 5 and 6, and the third row contains 7, 8 and 9.

Basic List Operations - length, indexing, slicing and traversing

Note: We perform these operations on lists in the same way as we do on strings. However, a list is mutable (its entries can be changed), while a string is immutable.

Length of a list: the len() function

>>> myList = ['a', 'b', 'c']
>>> print len(myList)
3

Indexing: The valid indices for a list L are 0 to len(L)-1. We can also use negative indices.

>>> a = [4, 2, 7 ]
>>> print a[0]
4
>>> print a[-1]
7
>>> print a[-2]
2
>>> a[1] = 10   # change the element at index 1 to 10
>>> print a
[4, 10, 7]

Access an entry in a 2-dimensional array by specifying first the row, and then the column, of the entry:

l2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
x = l2[0][0]    # x is 1
y = l2[1][2]    # y is 6

Slicing: get a sub-list

Notation: myList[start:end]
The resulting sub-list contains the values of myList from index start up to, but not including, end.

As with strings, the start or end index can be omitted.

>>> a = [4, 10, 7, 3]
>>> b = a[1:3]         # b = [10, 7]
>>> c = a[1:]           # c = [10, 7, 3]
>>> d = a[:-2]         # d = [4, 10]

Traversing a list: visit every list element in order

>>> list1 = [2, 3, 6, 0]
>>> for i in list1:
...        print i,
...
2 3 6 0

>>> a = [1, 13, 2, 6]
>>> length = len(a)
>>> for i in range(length):
...          a[i] = a[i] * 2
...
>>> print a
[2, 26, 4, 12]

Other List Operations

• value in list
• True if value occurs in list, False otherwise

>>> a = [1, 2, 3]
>>> if 2 in a:
...        print '2 is in the list'
...
2 is in the list

 Operation Description list.sort() sorts the entries into ascending order list.reverse() reverses the order of the list items list.index(x) returns the index of the first occurrence of x in list Error if x does not occur in list list.count(x) returns the number of occurrences of x in list list.pop(i) deletes and returns item at index i in list list.remove(x) removes 1st occurrence of x from list Error if x is not found in list del list[i:j] removes items from index i to index j-1 same as: list[i:j] = [] min(list) returns the smallest list item max(list) returns the largest list item