Dictionaries – Methods (keys, values, items, get)

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Dictionaries – Methods (keys, values, items, get)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does .keys() return?
A All the keys
B All the values
2. What does .values() return?
A All the values
B All the keys
3. What does .items() return?
A Key-value pairs as tuples
B Just keys
4. d.get('x', 0) what happens if 'x' doesn't exist?
A KeyError
B Returns 0 (the default)
5. d.get('x') what if 'x' doesn't exist and no default?
A KeyError
B Returns None

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1d = {'a': 1} d.get('b') print(d['b']) AOk: Loop over key-value pairs
2d = {'x': 5} d.pop('y') BError: Key 'y' not in dict
3d.get('missing', 0) CValid: Returns 0 if key absent
4for k, v in d.items(): print(k, v) DError: .get() doesn't add the key
5d = {'a':1,'b':2} print(list(d.keys())) EOutput: 99
6d = {'a':1} print(d.get('b', 99)) FOutput: ['a', 'b']

Definitions

.keys()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
d = {'a': 1, 'b': 2} print(d.____( ))
2.
d = {'a': 1, 'b': 2} print(d.______( ))
3.
d = {'a': 1} print(d.___('b', 0))
4.
d = {'x': 10} val = d.___('x')
5.
d = {'a': 1} d.______({'b': 2})

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A d = {'a': 1, 'b': 2}
B all_keys = d.key()
C print('Keys:', all_keys)
D print('Done')
Question 2
A d = {'a': 1, 'b': 2}
B all_vals = d.value()
C print('Values:', all_vals)
D print('Done')
Question 3
A d = {'a': 1, 'b': 2}
B all_items = d.item()
C print('Items:', all_items)
D print('Done')
Question 4
A d = {'a': 1, 'b': 2}
B val = d.get['b']
C print('Value:', val)
D print('Done')
Question 5
A result = {'a': 1, 'b': 2}.pop()
B label = 'Dict'
C print('Result:', result)
D print('Done')

Answer Key

Dictionaries – Methods (keys, values, items, get)
Section A: Quick Questions
Q1
A — All the keys
Q2
A — All the values
Q3
A — Key-value pairs as tuples
Q4
B — Returns 0 (the default)
Q5
B — Returns None
Section B: Match the Pairs
Q1
d = {'a': 1} d.get('b') print(d['b']) → Error: .get() doesn't add the key
Q2
d = {'x': 5} d.pop('y') → Error: Key 'y' not in dict
Q3
d.get('missing', 0) → Valid: Returns 0 if key absent
Q4
for k, v in d.items(): print(k, v) → Ok: Loop over key-value pairs
Q5
d = {'a':1,'b':2} print(list(d.keys())) → Output: ['a', 'b']
Q6
d = {'a':1} print(d.get('b', 99)) → Output: 99
Section C: Definitions
Q1
.keys() — Returns all keys in the dictionary.~~.values()
Section D: Fill in the Blanks
Q1
d = {'a': 1, 'b': 2} print(d.keys( ))
Q2
d = {'a': 1, 'b': 2} print(d.values( ))
Q3
d = {'a': 1} print(d.get('b', 0))
Q4
d = {'x': 10} val = d.pop('x')
Q5
d = {'a': 1} d.update({'b': 2})
Section E: Spot the Error
Q1
B — It's keys() with an 's': not key()
Q2
B — It's values() with an 's': not value()
Q3
B — It's items() with an 's': not item()
Q4
B — Use parentheses () to call methods, not square brackets []
Q5
A — dict.pop() requires a key argument: which key to remove?