A dictionary comprehension has syntax similar to a list comprehension. The expression is a key:value pair, and is added to the resulting dictionary. If a key is used more than once, it overrides any previous keys. this can be handy for building a dictionary from a sequence of values.
animals = ['OWL','Badger','bashbaby','Tiger','Wombat','GORILLA','AARDVARK']
>>> comprehensiond = {a.lower(): len(a) for a in animals}
>>> comprehensiond
{'owl': 3, 'badger': 6, 'bashbaby': 8, 'tiger': 5, 'wombat': 6, 'gorilla': 7, 'aardvark': 8}
>>> ages = {"Aaron":47, "Jenifer": 46, "Alex": 17, "Adam":14}
>>> for key, value in ages.items():
print(f"{key} is {value} years old")
Aaron is 47 years old
Jenifer is 46 years old
Alex is 17 years old
Adam is 14 years old
Hint: use pp to print out dictionaries to make them more readable.
from pprint import pprint as pp.
>>> from pprint import pprint as pp
>>> country_to_capital = {'United Kingdom':'London',
... 'Brazil':'Brazilia',
... 'Morocco':'Rabat',
... 'Sweden':'Stockholm'}
>>> #Convert the country_to_capital to capital_to_country
>>> capital_to_country = {capital: country for country, capital in country_to_capital.items()}
>>> pp(capital_to_country)
{'Brazilia': 'Brazil',
'London': 'United Kingdom',
'Rabat': 'Morocco',
'Stockholm': 'Sweden'}