Everything about lists

  • Lists are containers, containing items indexed by their location.

  • Location starts at 0 from left and -1 from right.

    >>> animals
    ['dog', 'cat', 'pig']
    >>> animals[0]
    'dog'
    >>> animals[1]
    'cat'
    >>> animals[2]
    'pig'
    >>> animals[3]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: list index out of range
    >>> animals[-1]
    'pig'
    >>> animals[-2]
    'cat'
    >>> animals[-3]
    'dog'
    >>> animals[-4]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: list index out of range
    
  • Valid list indices are between (0, len(L)-1) and (-1, -len(L)).

List operations

  • These operations do not return anything and alter the list that they are applied to (append, insert, remove, sort):

    >>> animals
    ['dog', 'cat', 'pig']
    >>> animals.append('cow')
    >>> animals
    ['dog', 'cat', 'pig', 'cow']
    >>> animals.insert(2,'bird')
    >>> animals
    ['dog', 'cat', 'bird', 'pig', 'cow']
    >>> animals.remove('cat')
    >>> animals.remove('cat')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: list.remove(x): x not in list
    >>> animals
    ['dog', 'bird', 'pig', 'cow']
    >>> animals.sort()
    >>> animals
    ['bird', 'cow', 'dog', 'pig']
    
  • This operation returns a value from the list and modifies the list:

    >>> animals
    ['dog', 'cat', 'pig', 'cow']
    >>> animal = animals.pop()
    >>> animals
    ['dog', 'cat', 'pig']
    >>> animal
    'cow'
    
  • These operations return a new list by copying the top level element in each list, hence called a shallow copy (+,*,list,sorted,slicing):

    >>> birds = ['cardinal','bluebird','sparrow']
    >>> animals = ['dog','cat','pig']
    >>>
    >>> birds + animals
    ['cardinal', 'bluebird', 'sparrow', 'dog', 'cat', 'pig']
    >>> multibird = birds*2
    >>> multibird
    ['cardinal', 'bluebird', 'sparrow', 'cardinal', 'bluebird', 'sparrow']
    >>> birds_copy = list(birds)
    >>> allanimals = birds + animals
    >>> allanimals.append('hippo')
    >>> someanimals = animals[1:3]
    >>> someanimals
    ['cat', 'pig']
    >>> allanimals
    ['cardinal', 'bluebird', 'sparrow', 'dog', 'cat', 'pig', 'hippo']
    >>> birds
    ['cardinal', 'bluebird', 'sparrow']
    >>> animals
    ['dog', 'cat', 'pig']
    >>> ### see original lists are not changed
    
  • Slicing produces a new list, by copy and pasting values from the original list, L[s:e:i], from starting index s, to ending index e and incrementing by i.

    >>> allanimals
    ['cardinal', 'bluebird', 'sparrow', 'dog', 'cat', 'pig', 'hippo']
    >>> allanimals[1:5:2]
    ['bluebird', 'dog']
    >>> allanimals[-2:-4]
    []
    >>> allanimals[-2:-4:-1]
    ['pig', 'cat']
    >>> allanimals[::-1]
    ['hippo', 'pig', 'cat', 'dog', 'sparrow', 'bluebird', 'cardinal']
    
  • You can apply the same principles to list of lists.

    >>> names = [ ['Sun', 500], ['Moon', 200] ]
    >>> names[0]
    ['Sun', 500]
    >>> names[0][0]
    'Sun'
    >>> names[0][1]
    500
    >>> names[1]
    ['Moon', 200]
    >>> names[1][-1]
    200
    >>> names[0][:1]
    ['Sun']
    >>> names[0].append(200)
    >>> names
    [['Sun', 500, 200], ['Moon', 200]]
    

    As this is a list of lists, then the 0th item is a list. You can apply the same list functions to this list as well.