Sorting lists#

When working with simple forms of data, e.g. a list of measurements, it might be useful to sort them. With this, we could for example quickly access the smallest numbers in a list.

Let’s start again with a list of numbers

data = [34, 0, 65, 23, 51, 9, 50, 78, 34, 100]
data
[34, 0, 65, 23, 51, 9, 50, 78, 34, 100]

There is the sort operation, which actually modifies a list:

data.sort()
data
[0, 9, 23, 34, 34, 50, 51, 65, 78, 100]

Hence, we should execute Jupyter notebook cells in order because the data variable is afterwards modified (we should do this anyway).

After sorting, this will give us the three smallest entries in the list:

data[:3]
[0, 9, 23]

And this will give us the three largest numbers:

data[-3:]
[65, 78, 100]

Exercise#

Find out the median of these two lists of numbers:

data1 = [6, 4, 3, 4, 8, 10]
data2 = [6, 4, 3, 4, 8, 10, 8]