Sorting things#

A functionality which comes up often is the need to sort things. Here we give some examples of doing this for the various data types.

Sorting a list#

# Make a random list of ints
import random
l = []
for i in range(21):
    l.append(random.randint(1,100))
print(f'Original = {l}')
l = sorted(l)
print(f'Sorted = {l}')
Original = [34, 100, 77, 91, 54, 94, 2, 51, 9, 61, 16, 12, 77, 20, 27, 85, 42, 3, 4, 8, 2]
Sorted = [2, 2, 3, 4, 8, 9, 12, 16, 20, 27, 34, 42, 51, 54, 61, 77, 77, 85, 91, 94, 100]
# Make a list of all capital letters
s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
letters = []
for letter in s:
    letters.append(letter)
print(f'Alphabet: {letters}')
# Choose randomly from it and make another list
l1 = []
for i in range(21):
    l1.append(random.choice(letters)) 
print(f'Original: {l1}')
l2 = sorted(l1)
print(f'Sorted: {l2}')
Alphabet: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
Original: ['F', 'L', 'S', 'N', 'X', 'A', 'K', 'Z', 'C', 'J', 'D', 'H', 'G', 'M', 'X', 'Y', 'I', 'W', 'A', 'E', 'C']
Sorted: ['A', 'A', 'C', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'S', 'W', 'X', 'X', 'Y', 'Z']

Sort a dictionary#

Create an ordered dictionary (both keys and values):

d = {'A': 5,
     'E': 1, 
     'B': 2, 
     'F': 9, 
     'C': 1, 
     'G': 3, 
     'D': 0, 
     'H': 4
    }

Sorting by keys is simple and is almost what you would expect from the list examples above. The change is that running sorted() with a dictionary argument returns a list of key/value pairs:

print(sorted(d.items()))
[('A', 5), ('B', 2), ('C', 1), ('D', 0), ('E', 1), ('F', 9), ('G', 3), ('H', 4)]

So you need to create a dict again by passing that list to dict():

d_sorted = dict(sorted(d.items()))
print(f'Original: {d}')
print(f'Sorted: {d_sorted}')
Original: {'A': 5, 'E': 1, 'B': 2, 'F': 9, 'C': 1, 'G': 3, 'D': 0, 'H': 4}
Sorted: {'A': 5, 'B': 2, 'C': 1, 'D': 0, 'E': 1, 'F': 9, 'G': 3, 'H': 4}

Sorting by the values is a little trickier. One has recognize that recall that d.items() returns a list of tuples:

print(list(d.items()))
for item in d.items():
    print(f'item is {type(d)} with {item[0]=} and {item[1]=}')
[('A', 5), ('E', 1), ('B', 2), ('F', 9), ('C', 1), ('G', 3), ('D', 0), ('H', 4)]
item is <class 'dict'> with item[0]='A' and item[1]=5
item is <class 'dict'> with item[0]='E' and item[1]=1
item is <class 'dict'> with item[0]='B' and item[1]=2
item is <class 'dict'> with item[0]='F' and item[1]=9
item is <class 'dict'> with item[0]='C' and item[1]=1
item is <class 'dict'> with item[0]='G' and item[1]=3
item is <class 'dict'> with item[0]='D' and item[1]=0
item is <class 'dict'> with item[0]='H' and item[1]=4

What sorted will by default do (and did above in the example with sorting dictionary keys) is sort on the first element of the tuples. It only look at the 2nd element if the 1st one is identical, but by construction that doesn’t happen when the list of tuples is returned as key/value pairs from calling ``items() on a dictionary since keys are unique.

So what we need to do is tell sorted() to use the 2nd element in the tuple (item[1]). We won’t explain the full detail here, but this is done via passing a function (here a sort-of “inline” caleld a “lambda”) as an additional argument to sorted(). We add also reverse=True as an additional option in this example.

So, long story short, one needs to do:

d_sorted = dict(sorted(d.items(), key=lambda item: item[1], reverse=True))
print(f'Original: {d}')
print(f'Sorted: {d_sorted}')
Original: {'A': 5, 'E': 1, 'B': 2, 'F': 9, 'C': 1, 'G': 3, 'D': 0, 'H': 4}
Sorted: {'F': 9, 'A': 5, 'H': 4, 'G': 3, 'B': 2, 'E': 1, 'C': 1, 'D': 0}

Sorting in Numpy#

#  TO ADD