Lists

Dr Andy Evans

[Fullscreen]

Lists

  • Like tuples, but mutable.
  • Formed with brackets:
    Assignment:
    >>> a = [1,2,3]
    Or with a constructor function:
    a = list(some_other_container)
  • Subscription (again, zero based indexing):
    >>> a[0]
    1
    >>> a[len(a) - 1]
    3

Assignment

  • You can't assign to tuples, but you can lists:
    a = (1,2,3)
    a[0] = 10 # Breaks the program.
    a = [1,2,3]
    a[0] = 10 # Fine

    a = [10,20,30]
    # Assigns a new list,
    # rather than altering the old one.
    Essentially the same kind of operation as :
    b = 100
    b = 200
  • For mutables, change a variable's content, it changes for all attached labels. You can still connect it to something new.
    >>> a = [10] # List containing 10.
    >>> b = a
    >>> b[0]
    10
    >>> b[0] = 20
    >>> a[0]
    # Changing b changes a; refer to the same
    20 # object.
    >>> b = [30]
    # Disconnect b from [10] and attach
    # it to a new list. Essentially a new "b". >>> a[0]
    10
    >>> b[0]
    30

Slices

  • Extended indexing is a way of referencing values in a sequence.
  • These are sometimes called a 'slice' (essentially slice objects are generated, containing the indices generated by a range).
  • a[i:j] returns all the elements between i and j, including a[i] but not a[j]
    a[i:j:k] returns the same, but stepping k numbers each time.
  • Slices must have at least [:] (slice everything), but the rest is optional.
    • If j > len(a), the last position is used.
    • If i is None or omitted, zero is used.
    • If i > j, the slice is empty.
  • What's important is the position of the colons.
    a[:2] # First two values.
    a[-2:] # Last two values.
  • While you can use slices as indices with immutable sequences, they can be used with mutable sequences like lists for assignment as well.
    >>> a = [0,1,2,3,4]
    >>> b = [10,20,30]
    >>> a[1:3] = b
    >>> a
    [0,10,20,30,3,4]
    # Note we replace 2 values with 3.

    >>> a = [0,1,2,3,4]
    >>> b = [10,20]
    >>> a[1:5] = b
    >>> a
    [0,10,20]
    # We replace 4 values with 2.

Functions

  • All the same functions as immutable sequences, plus these "in place" functions (i.e. functions that change the original, rather than returning a copy):
    del(a[:]) Delete a slice.
    a.clear() Empty the list.
    a.extend(b) Extends a with the contents of b (i.e. a += b)
    a.insert(i, x) Inserts x into a at the index position i.
    a.pop(i) Returns the item at i and removes it from a.
    a.remove(x) Remove the first item from a equal to x
    a.reverse() Reverses the items of a.

2D lists

  • There is nothing to stop sequences containing other sequences.
  • You can therefore built 2D (or more) sequences.
    a = [[1,2,3],[10,20,30],[100,200,300]]
  • They can be regular (i.e. have all second dimension sequences the same length) or irregular.
  • Reference like this:
    >>> a[0]
    [1,2,3]
    >>> a[0][0]
    1
  • Note that tuples can contain lists. This seems odd, as lists are mutable, but it is only the object references/links in the tuple that are immutable, not the objects themselves. Tuples of numbers and strings are immutable because numbers and strings are immutable.
    >>> a = ([1,2],[10,20])
    >>> a[0][0] = 100
    >>> a
    ([100,2],[10,20])
    >>> a[0] = [1000,2000]
    TypeError: 'tuple' object does not support item assignment

  • Finally, you can combine two lists or tuples into a list of tuples like this:
    >>> a = [1,2,3,4,5]
    >>> b = [10,20,30,40,50]
    >>> c = zip(a,b)
    >>> d = list(c)
    >>> d
    [(1,10),(2,20),(3,30),(4,40),(5,50)]
  • As we'll see in the module on control flow, this allows us to process two sets of number simultaneously.

Command line arguments

  • When you run a Python program at the command line, you can pass information into it.
  • This is not unusual. When you click on a Word file in Windows, what is happening in the background is Word is being activated and passed the filename to open.
  • Indeed, when you run a Python program, you are doing this:
    python helloworld.py
    you're saying "run the python program, and the thing after its name is the file for it to execute."
  • The elements passed into a program like this are called "command line arguments". They are used to quickly say how a program should work without re-writing the code.
  • We can actually get our code to record command line arguments.
    python ourfile.py arg1 arg2 arg3
  • Inside the program, these are made available in a list called sys.argv
  • argv[0] is actually the script name (and, depending on OS, path) If run using the -c option of the interpreter, argv[0] == "-c".
  • After argv[0] come the other arguments as strings.
  • Example use:
    #args.py
    import sys
    print ("Hello " + sys.argv[1])

    > python args.py Dave
    > Hello Dave