Python Arena
Step 2 of 3

Dictionary comprehensions

In part 5 you built dictionaries with a loop: start from an empty dictionary, and in each round store a value under a key. This one turns temperatures in Celsius into Fahrenheit:

celsius = [0, 25, 100]
fahrenheit = {}
for c in celsius:
    fahrenheit[c] = c * 9 / 5 + 32
print(fahrenheit)
Output
{0: 32.0, 25: 77.0, 100: 212.0}

A dictionary comprehension says the same in one line. It's written in curly brackets, like a dictionary, and in front of the for it has a key and a value with a colon between them, like a pair in a dictionary:

celsius = [0, 25, 100]
fahrenheit = {c: c * 9 / 5 + 32 for c in celsius}
print(fahrenheit)
Output
{0: 32.0, 25: 77.0, 100: 212.0}

The shape is:

{key: value for item in series}

Compare it with the loop: what comes before the colon, c, is what went between the square brackets in fahrenheit[c], and what comes after it is the value that was stored there. A filter with if at the end works just as in a list comprehension.

A key only once

A dictionary has each key only once. When a comprehension makes the same key again, the new value replaces the old one, and the key keeps its place from the first time. Here every fruit goes under its first letter:

fruits = ["apple", "avocado", "banana", "blueberry", "cherry"]
by_letter = {fruit[0]: fruit for fruit in fruits}
print(by_letter)
Output
{'a': 'avocado', 'b': 'blueberry', 'c': 'cherry'}

"apple" went in first, under "a", and then "avocado" took its place. Five fruits went in, but the dictionary has only three keys, and for each letter the last fruit wins.

From a dictionary to a dictionary

To go through a dictionary's keys and values together, use its items() method, as in a for loop. Then the new dictionary can change the values, leave some pairs out, or even swap the keys and values:

In the Python interpreter
>>> prices = {"tea": 3, "cake": 5, "soup": 8}
>>> {name: price * 2 for name, price in prices.items()}
{'tea': 6, 'cake': 10, 'soup': 16}
>>> {name: price for name, price in prices.items() if price < 6}
{'tea': 3, 'cake': 5}
>>> {price: name for name, price in prices.items()}
{3: 'tea', 5: 'cake', 8: 'soup'}

Two lists of the same length can become one dictionary, with the indexes from range(len(...)):

names = ["Ada", "Alan", "Grace"]
born = [1815, 1912, 1906]
years = {names[i]: born[i] for i in range(len(names))}
print(years)
Output
{'Ada': 1815, 'Alan': 1912, 'Grace': 1906}

Curly brackets, not square ones

A key and a value with a colon only work inside curly brackets. In square brackets, Python stops at the colon:

celsius = [0, 25, 100]
fahrenheit = [c: c * 9 / 5 + 32 for c in celsius]
Python stops with
  File "main.py", line 2
    fahrenheit = [c: c * 9 / 5 + 32 for c in celsius]
                   ^
SyntaxError: invalid syntax

Your task

Challenge 1 of 3

Fill in the blank so that the dictionary has the numbers 1 to 5 as its keys and each number's square as its value. The program should print:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Challenge 2

What does this program print? Type each line it prints.

Challenge 3

Write a function passed(scores, limit) that gets a dictionary of names and scores, and returns a new dictionary with only the people whose score is at least limit. Use a dictionary comprehension.

For example, this call:

print(passed({"Ann": 72, "Bo": 45, "Cy": 90}, 50))

prints:

{'Ann': 72, 'Cy': 90}