Lists#

If we want to store many items, we can create many variables. However, this quickly becomes tedious and error-prone. Instead, we should group our data items together in collections. Lists are the standard collections in Python.

Making Lists#

Lists can contain any python data type. We make lists by placing items in square brackets, [ ], separated by commas.

numbers = [1, 2, 3]

We can also make an empty list:

judges = []

Let’s look at the list of judges from a recent ECtHR case.

judges = ['Anne Grøstad', 'Ivana Jelić', 'Jovan Ilievski', 'Lətif Hüseynov',
          'Mārtiņš Mits', 'Stéphanie Mourou-Vikström', 'Síofra O’Leary', 'Victor Soloveytchik']

Accessing list elements#

We can access list elements by using indexes in square brackets. This is like with strings, where we can access individual characters by using indexes.

print(judges[0])
Anne Grøstad

Note

Remember that Python counts from 0, not 1.

We can also count backwards from the last element with negative indexes.

print(judges[-1])
Victor Soloveytchik

We can get a new list containing a part of the original list. This is called a list slice, and we get it by using two indexes, a from index and a to index:

selection = judges[2:4]
print(selection)
['Jovan Ilievski', 'Lətif Hüseynov']

List Operations#

Python list objects have many methods for manipulating their content. There are also built-in list functions. Here, we’ll look at a few of these methods and functions.

Length#

As with strings, we can use the function len() to get the length of a list.

print(len(judges))
8

Append#

We can append items to a list. Here we use examples from the joined cases C-403/08 and C-429/08 (Premier League).

defendants = ['QC Leisure']
defendants.append('SR Leisure Ltd')
defendants.append('AV Station plc')
print(defendants)
['QC Leisure', 'SR Leisure Ltd', 'AV Station plc']

Insert#

If the order of the list items is significant, we can insert an element at a specific place:

defendants.insert(0, 'Owen')
print(defendants)
['Owen', 'QC Leisure', 'SR Leisure Ltd', 'AV Station plc']

Finding items#

There are two ways to check if a list contains an item. First, we can use the in operator that we have already seen:

print('Owen' in defendants)
True

If we need to find the position of the item, we can use <listname>.index().

print('Owen is defendant number', defendants.index('Owen'))
Owen is defendant number 0

Removing items#

We can remove a given list item:

item = 'Owen'
if item in defendants:
    defendants.remove(item)
print(defendants)
['QC Leisure', 'SR Leisure Ltd', 'AV Station plc']

We can also remove an item at a given position with the method <listname>.pop(index). This method returns the element which was removed, so we can use it if necessary. The most common would be to remove the first or last element:

print(defendants)
defendant = defendants.pop(0)
print(defendant)
print(defendants.pop(-1))
print(defendants)
['QC Leisure', 'SR Leisure Ltd', 'AV Station plc']
QC Leisure
AV Station plc
['SR Leisure Ltd']

Nested Lists#

Lists can contain other lists, and these are called nested lists. For example, we could have a case where there are two groups of defendants.

defendants = [['QC Leisure', 'SR Leisure Ltd', 'AV Station plc', 'Owen'],
              ['Media Protection Services Ltd']]
print(defendants)
[['QC Leisure', 'SR Leisure Ltd', 'AV Station plc', 'Owen'], ['Media Protection Services Ltd']]

We can access the elements one step at a time, or by using multiple indexes:

defendant_group = defendants[0]
print(defendant_group[0])
# is equivalent to
print(defendants[0][0])
QC Leisure
QC Leisure

List References#

Unlike simple Python variables, list variables contain references to lists, not the lists themselves. If we assign the value of one list variable to another, we don’t get a copy of the list. Instead, both variables refer to the same list, and we can call them aliases.

defendants = ['QC Leisure', 'SR Leisure Ltd']
defendants2 = defendants
defendants.append('AV Station plc')
print('defendants: ', defendants)
print('defendants2:', defendants2)
defendants:  ['QC Leisure', 'SR Leisure Ltd', 'AV Station plc']
defendants2: ['QC Leisure', 'SR Leisure Ltd', 'AV Station plc']

Since both variables refer to the same list, when we modify one of them, the change applies to both.

Copying Lists#

We can also make a new copy of a list, by passing it to the function list(). Then, the variables refer to different lists, and we can modify them independently:

defendants2 = list(defendants)
defendants2.pop()
print('defendants: ', defendants)
print('defendants2:', defendants2)
defendants:  ['QC Leisure', 'SR Leisure Ltd', 'AV Station plc']
defendants2: ['QC Leisure', 'SR Leisure Ltd']

Characters#

Strings are made up of Unicode characters. Strings aren’t really lists but sequences of characters. However, in some ways they behave like lists. We can get a single character by using an index in brackets:

name = 'John Doe'
print(name[0])
J

Note

As with lists, the first letter (“J”) has index zero. Python always starts counting from zero.

We can also count backwards from the end with negative indexes, just like with lists:

name = 'John Doe'
print(name[-1])
e