Dictionaries

Lists in Python are ordered sets of objects that you access via their position/index. Dictionaries are unordered sets in which the objects are accessed via their keys. In other words, dictionaries are unordered key-value pairs.

favs = {'mary': 'orange', 'john': 'green', 'eric': 'blue'}
favs
favs['john']      # returns 'green'
favs['mary']      # returns 'orange'
list(favs.values()).index('blue')     # will return the index of the first value 'blue'
for key in favs:
	print(key)            # will print the names (keys)
	print(favs[key])      # will print the colours (values)
for k in favs.keys():
	print(k, favs[k])     # the same as above
for v in favs.values():
	print(v)              # cycle through the values
for i, j in favs.items():
	print(i,j)            # both the names and the colours

Now let’s see how to add items to a dictionary:

concepts = {}
concepts['list'] = 'an ordered collection of values'
concepts['dictionary'] = 'a collection of key-value pairs'
concepts

Let’s modify values:

concepts['list'] = 'simple: ' + concepts['list']
concepts['dictionary'] = 'complex: ' + concepts['dictionary']
concepts

Deleting dictionary items:

del concepts['list']       # remove the key 'list' and its value

Values can also be numerical:

grades = {}
grades['mary'] = 5
grades['john'] = 4.5
grades

And so can be the keys:

grades[1] = 2
grades

Sorting dictionary items:

favs = {'mary': 'orange', 'john': 'green', 'eric': 'blue', 'jane': 'orange'}
sorted(favs)             # returns the sorted list of keys
sorted(favs.keys())      # the same
for k in sorted(favs):
	print(k, favs[k])         # full dictionary sorted by key
sorted(favs.values())         # returns the sorted list of values
Question 4c

Write a script to print the full dictionary sorted by the value.

Hint: create a list comprehension looping through all (key,value) pairs and then try sorting the result.

 

Similar to list comprehensions, we can form a dictionary comprehension:

{k:'.'*k for k in range(10)}
{k:v*2 for (k,v) in zip(range(10),range(10))}
{j:c for j,c in enumerate('computer')}