Chapter 3 · Data

Dictionaries and sets

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 20 · Concept

A dictionary finds a value by its key

In this lesson you look values up by a key and count things. You also keep each item once, in a set.

ages = {"Ada": 36, "Alan": 41}
print(ages["Ada"])
  • A dictionary pairs each key with a value: "Ada" with 36.
  • ages["Ada"] finds the value of the key "Ada", so it prints 36.
ages = {"Ada": 36, "Alan": 41}
ages["Ada"] = 37
ages["Grace"] = 85
print(ages["Ada"], len(ages))
print(ages.get("Bo", 0))
print("Alan" in ages, 41 in ages)
print(ages)
phones = {"Ada": "555-0101", "Alan": "555-0199"}
# 1. add Grace
# 2. change Alan's number
# 3. print Grace, Alan and Bo
# 4. print how many names
ages = {"Ada": 36, "Alan": 41, "Grace": 85}
del ages["Ada"]
ages["Ada"] = 37
ages["Alan"] = 42
for name, age in ages.items():
    print(name, age)
print(sorted(ages))
print(sum(ages.values()))
marks = {"Ada": [90]}
marks["Ada"].append(85)
print(marks)
text = "red blue red green blue red"
counts = {}
# count each word of text in counts,
# then print each word with its count
where = {}
where[(2, 3)] = "treasure"
print(where[(2, 3)])
key = ["a", "b"]
where[key] = 1
words = "the cat and the hat".split()
unique = set(words)
print(len(words), len(unique))
print(sorted(unique))
pets = set()
pets.add("dog")
pets.add("cat")
pets.add("dog")
print(len(pets), "cat" in pets)
print(sorted(unique & pets))
print(sorted(unique | pets))
grades = {"Lin": [90, 80], "Ada": [70, 85, 100], "Bo": [60, 75]}
# 1. add Cy with the scores 88 and 92
# 2. add the score 95 to Bo's list
# 3. print each name, in order, with its average
words = ["listen", "silent", "cat", "act", "dog", "enlist"]
groups = {}
# put each word in its group of anagrams,
# then print each group of two or more words
Terminal
Step 1 of 8Lookups

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)