Lists

In Python, a list is an dynamic ordered collection of items of any type. Dynamic means that the size of the list can be changed by adding or removing items. If you already know about arrays you know that an array is an ordered collection of items of the same type. Arrays are sometimes also dynamic but not always.

# create empty list
myList = []

# add some elements
myList.append(4)
myList.append(7)
myList.append(1)
myList.append(3)
myList.append(9)

# alternatively, create and initialise with some values
myList=[1,2,3,4,5,6]

# show contents of myList
myList

# show length of myList
len(myList)

# show individual elements and a range
myList[1] - one element
myList[2] - one element
myList[:3] - range of elements 0, 1 and 2
myList[2:] - range of elements 2 and further to the end of the list

# Useful way to reverse an list: negative step size
# This does not modify the list but returns the reversed version.
def reverse_list(x):
  return x[::-1]

List searching

http://www.effbot.org/zone/python-list.htm#searching

  if value in L:
    print "The list contains", value
    # get the element's index
    i = L.index(value)