Python lists

  1. Let's start out with a string containing the names of the digits

    number_string = 'zero one two three four five six seven eight nine'
    
  2. We can split the string into at every white space and get a list of strings

    number_list = number_string.split()
    number_list
    ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    
  3. We could also have split on other characters than spaces, for example on every 'e' character

    number_string.split('e')
    ['z', 'ro on', ' two thr', '', ' four fiv', ' six s', 'v', 'n ', 'ight nin', '']
    

    but that's not so easy to read.

  4. We can ask how long our list is using the len operator

    len(number_list)
    10
    
  5. We can access individual members of our list by the number of its index

    number_list[0]
    'zero'
    number_list[4]
    'four'
    
  6. If we give a negative index it counts from the right end instead of the left

    number_list[-2]
    'eight'
    
  7. We can also use ranges of indices to select sublists

    number_list[3:5]
    ['three', 'four']
    

    Note that the sublist includes the element at the first index and excludes the index at the last index.

  8. In fact we can use indexing to step through the list by twos or threes or whatever step size we choose

    number_list[1:9:2]
    ['one', 'three', 'five', 'seven']
    number_list[1:9:3]
    ['one', 'four', 'seven']
    

    The colon (:) is called the slice operator since we use it to slice and dice lists in various ways.

  9. Read the notation number_list[1:9:2] as start, stop, step. It starts at 1, it stops before it hits 9 and it steps by 2.
  10. Python lists are mutable, which means that we change parts of a list by assignment.

    number_list[5]
    'five'
    number_list[5] = 'vijf'
    number_list
    ['zero', 'one', 'two', 'three', 'four', 'vijf', 'six', 'seven', 'eight', 'nine']
    
  11. We can even change slices

    number_list[1:9:3] = ['een', 'vier', 'zeven']
    number_list
    ['zero', 'een', 'two', 'three', 'vier', 'vijf', 'six', 'zeven', 'eight', 'nine']
    
  12. Or use the empty slice to create a copy of the entire list

    nummer_lijst = number_list[:]
    
  13. You can find out what else you can do with a list by asking it what its directory of names is:

    dir(number_list)
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
     '__delslice__', '__doc__', '__eq__', '__format__', '__ge__',
     '__getattribute__', '__getitem__', '__getslice__', '__gt__',
     '__hash__', '__iadd__', '__imul__', '__init__', '__iter__',
     '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__',
     '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__',
     '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__',
     '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert',
     'pop', 'remove', 'reverse', 'sort']
    
  14. You can ask the list what each of these functions do. Ignore the ones with underscores in their names for the time being.

    help(number_list.insert)
    Help on built-in function insert:
    
    insert(...)
        L.insert(index, object) -- insert object before index
    

    Let's try it out

    number_list.insert(4, 'pi')
    number_list
    ['zero', 'een', 'twee', 'drie', 'pi', 'vier', 'vijf', 'six', 'zeven', 'eight', 'nine']
    

    Experiment for yourself with the other list functions or just look them up if you're lazy.

Author: Breanndán Ó Nualláin <o@uva.nl>

Date: 2025-09-04 Thu 08:55