Lists

In most languages a collection of homogeneous (all of the same type) entities is called an array. The size of the array is fixed at the time of creation, however, the contents of the array can be changed during the course of the execution of the program. Higher dimensional arrays are also possible, where each element of an array is an array.

The analogue of an array in Python is a list. Even though a list defines a collection of things it has different properties from an array. A list could be a collection of heterogeneous (different types) items. The size of a list is dynamic. It is not specified at the time of creation and can grow or shrink as needed. A list could have duplicate items. The order of the items in a list is important and not their uniqueness. Python also provides built-in functions to manipulate a list and its contents. A higher dimensional list has elements that are themselves lists. Given the flexibility and the associated functions, a Python list is a more powerful data structure than an array.

List Creation

There are several ways in which to create a list. You can enumerate all the elements of a list or create an empty list and then append or insert items into the list. When you append an item to a list, that item is added to the end of the list. To insert an item into a list you must specify its position and then all the elements to the right or below it are shifted to make space for it.

  # Enumerate the items
  a = [1, 2, 3]

  # Create an empty list and append or insert
  a = []
  a.append(1)            #  a = [1]
  a.append(2)            #  a = [1, 2]
  a.insert(1, 3)         #  a = [1, 3, 2]

  # Create a two dimensional list
  b = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
Note that the positions of items in a list start at an index value of 0. You can also create a list by concatenating two or more lists together. You can initialize a list with a predefined value.
  a = [1, 2]
  b = [4, 5]
  c = a + b               #  c = [1, 2, 4, 5]

  d = [0] * 5             #  d = [0, 0, 0, 0, 0]

Basic List Manipulations

To obtain the length of a list you can use the len() function.

  a = [1, 2, 3]
  length = len (a)         # length = 3
The items in a list are indexed starting at 0 and ending at index length - 1. You can also use negative indices to access elements in a list. For example a[-1] returns the last item on the list and a[-length] returns the first. Unlike a string, a list is mutable, i.e. its contents can be changed like so:
  a[1] = 4                 # a = [1, 4, 3]
To access or change an element in a 2-dimensional list specify the row first and then the column.
  d = b[1][2]               #  d = 6
  b[2][1] = b[1][2] * 2
You can also slice a list by specifying the starting index and the ending index and Python will return to you a sub-list from the starting index and upto but not including the end index.
  a = [1, 9, 2, 8, 3, 7, 4, 6, 5]
  b = a[2:5]                # b = [2, 8, 3]

List Traversal

One of the most important operations that you can do with a list is to traverse it, i.e. visit each and every element in the list in order. There are several ways in which to do so:

  a = [9, 2, 6, 4, 7]
  for item in a:
    print item,               # 9 2 6 4 7

  # Doubles each item in the list
  length = len (a)
  for i in range (length):
    a[i] = a[i] * 2          

Other List Functions

Function Meaning
list.sort() Sorts a list in ascending order
list.reverse() Reverses the elements in a list
value in list Returns True if the value is in the list and False otherwise
list.index(x) Returns the index of the first occurence of x. Use with the above function to check if x is in the list before determining its position.
list.count(x) Returns the number of occurences of x in the list
list.remove(x) Deletes the first occurence of x in list
list.pop(i) Deletes the ith element in the list and returns its value