Playing as a guest? to earn XP and track your progress.
๐ Dictionaries Quiz
๐ Dictionaries
A dictionary in Python stores data as:
key : value pairs
Rows and columns
Only values
Only keys
Each item of a dictionary is a key and its value, like "Riya": 92.
Which brackets are used to create a dictionary?
Angle brackets < >
Square brackets [ ]
Curly brackets { }
Round brackets ( )
Example: d = {"Riya": 92, "Aman": 78}.
Which symbol separates a key from its value in a dictionary?
Comma ,
Colon :
Equals =
Semicolon ;
Pairs are written as key: value, separated by commas.
Which statement about dictionary KEYS is TRUE?
Keys must be unique and immutable
Keys must be lists
Keys must be numbers
Keys can be repeated
Keys can be strings, numbers or tuples, but never repeated or mutable.
Which of these CANNOT be used as a dictionary key?
An integer
A tuple
A list
A string
A list is mutable, so it cannot be a key.
What does d = {[1, 2]: "a"} give?
TypeError
{[1, 2]: 'a'}
SyntaxError
KeyError
A list is unhashable (mutable) and cannot be a key.
Can two different keys have the SAME value in a dictionary?
No, values must be unique
Only if they are numbers
Yes, values can repeat
Only in an empty dictionary
Only keys must be unique; values can repeat.
Is a dictionary mutable?
Yes, pairs can be added, changed and deleted
No, it is fixed
Only after sorting
Only its keys can change
A dictionary is mutable.
What is the output of:
d = {"a": 1, "a": 2}
print(d){'a': 1}
Error
{'a': 2}
{'a': 1, 'a': 2}
A repeated key keeps only the LAST value.
Which statement creates an EMPTY dictionary?
d = []
d = ()
d = {}
d = dict[]
{} or dict() makes an empty dictionary.
What is the output of:
print(dict(a=1, b=2))[('a', 1), ('b', 2)]
{'a': 1, 'b': 2}
{a: 1, b: 2}
Error
dict() with keyword arguments makes string keys.
What is the output of:
print(dict([("x", 10), ("y", 20)])){'x', 10, 'y', 20}
{'x': 10, 'y': 20}
[('x', 10), ('y', 20)]
Error
dict() can make a dictionary from a list of (key, value) pairs.
d = {"Riya": 92, "Aman": 78}. What is d["Aman"]?
78
Aman
92
Error
A value is accessed using its key.
d = {"Riya": 92}. What does d["Zoya"] give?
None
IndexError
KeyError
0
Accessing a key that doesn't exist gives KeyError.
d = {"Riya": 92}. What does d[0] give?
Riya
92
IndexError
KeyError
Dictionaries don't use positions; 0 is treated as a key, and there is no such key.
d = {"Riya": 92}. What is the output of print(d.get("Zoya"))?
0
None
Zoya
KeyError
get() returns None instead of an error when the key is missing.
d = {"Riya": 92}. What is the output of print(d.get("Zoya", 0))?
None
92
0
KeyError
The second argument of get() is the default value.
What is the output of:
d = {"a": 1}
d["b"] = 2
print(d){'a': 1}
{'b': 2}
KeyError
{'a': 1, 'b': 2}
Assigning to a new key ADDS a new pair.
What is the output of:
d = {"a": 1}
d["a"] = 5
print(d){'a': 1}
{'a': 5}
Error
{'a': 1, 'a': 5}
Assigning to an existing key CHANGES its value.
What is the output of:
d = {"a": 1, "b": 2}
d.update({"b": 3, "c": 4})
print(d){'a': 1, 'b': 2, 'b': 3, 'c': 4}
{'a': 1, 'b': 3, 'c': 4}
{'a': 1, 'b': 2, 'c': 4}
{'b': 3, 'c': 4}
update() changes existing keys and adds new ones.
What is the output of:
d = {"a": 1, "b": 2}
del d["a"]
print(d)Error
{}
{'b': 2}
{'a': 1}
del d[key] removes that pair.
What is the output of:
d = {"a": 1, "b": 2}
x = d.pop("b")
print(x, d)b {'a': 1}
None {'a': 1}
2 {'a': 1}
2 {'a': 1, 'b': 2}
pop(key) removes the pair and returns its value.
What is the output of:
d = {"a": 1, "b": 2}
print(d.popitem())b
('b', 2)
2
('a', 1)
popitem() removes and returns the LAST inserted pair as a tuple.
What is the output of:
d = {"a": 1}
d.clear()
print(d)None
{}
Error
{'a': 1}
clear() removes all pairs but the dictionary still exists.
After del d (without brackets), what happens when you print(d)?
KeyError
{}
None
NameError
del d removes the dictionary name itself.
What is the output of:
d = {"Riya": 92, "Aman": 78}
print("Riya" in d)92
Error
True
False
in checks the KEYS of a dictionary.
What is the output of:
d = {"Riya": 92, "Aman": 78}
print(92 in d)True
False
Error
Riya
in checks only keys, not values.
How can you check whether 92 is one of the VALUES of d?
d.get(92)
92 in d
92 in d.values()
92 in d.keys()
d.values() gives all values, so in works on them.
What is the output of:
d = {"x": 1, "y": 2}
for k in d:
print(k, end=" ")1 2
x y
('x', 1) ('y', 2)
x 1 y 2
Looping over a dictionary gives its keys.
What is the output of:
d = {"x": 1, "y": 2}
for k in d:
print(d[k], end=" ")Error
x 1 y 2
x y
1 2
d[k] gives the value for each key.
What is the output of:
d = {"x": 1, "y": 2}
for k, v in d.items():
print(k, v, end=" ")x y 1 2
x 1 y 2
1 2
('x', 1) ('y', 2)
items() gives each (key, value) pair.
What is the output of:
print(list({"a": 1, "b": 2}.keys()))['a', 'b']
['a', 1, 'b', 2]
[('a', 1), ('b', 2)]
[1, 2]
keys() gives all the keys.
What is the output of:
print(list({"a": 1, "b": 2}.values()))['a', 'b']
[('a', 1), ('b', 2)]
[1, 2]
3
values() gives all the values.
What is the output of:
print(list({"a": 1, "b": 2}.items()))['a', 'b']
[1, 2]
{'a': 1, 'b': 2}
[('a', 1), ('b', 2)]
items() gives (key, value) tuples.
What is the output of:
print(len({"a": 1, "b": 2, "c": 3}))2
6
3
Error
len() gives the number of key-value pairs.
What is the output of:
print(max({"Riya": 92, "Zoya": 85, "Aman": 78}))92
Riya
Aman
Zoya
max() on a dictionary works on the KEYS (alphabetical order).
What is the output of:
print(sorted({"b": 2, "c": 1, "a": 3}))['a', 'b', 'c']
[3, 2, 1]
{'a': 3, 'b': 2, 'c': 1}
[1, 2, 3]
sorted() on a dictionary returns a sorted list of its keys.
What is the output of:
freq = {}
for ch in "aba":
freq[ch] = freq.get(ch, 0) + 1
print(freq){'a': 2, 'b': 1, 'a': 1}
{'a': 2, 'b': 1}
KeyError
{'a': 1, 'b': 1}
get(ch, 0) starts a new character at 0, then adds 1 each time.
Why does freq[ch] = freq[ch] + 1 give an error for a NEW character?
Strings can't be keys
The key does not exist yet, so KeyError occurs
freq is immutable
- doesn't work on numbers
Use freq.get(ch, 0) + 1 to handle new keys.
What is the output of:
words = "to be or not to be".split()
d = {}
for w in words:
d[w] = d.get(w, 0) + 1
print(d["to"], d["or"])2 1
6 1
2 2
1 1
"to" appears twice and "or" appears once.
What is the output of:
m = {"Riya": 92, "Aman": 78, "Zoya": 85}
top = ""
best = 0
for n in m:
if m[n] > best:
best = m[n]
top = n
print(top)Aman
Zoya
Riya
92
Riya has the highest marks (92).
What is the output of:
m = {"A": 85, "B": 60, "C": 90}
for k in m:
if m[k] >= 80:
print(k, end=" ")B
A C
A B C
85 90
Only A (85) and C (90) are 80 or more.
A list vs a dictionary โ which is TRUE?
A dictionary uses [ ] brackets
A list is accessed by index; a dictionary is accessed by key
A list stores key-value pairs
Both use index numbers
Lists use positions starting at 0; dictionaries use keys.
Which is the best choice to store the phone numbers of friends by their names?
Dictionary
String
List
Integer
A dictionary lets you look up the number directly with the name as the key.
What is the output of:
d = {1: "one", 2: "two"}
print(d[2])1
two
one
KeyError
Here 2 is a KEY, not a position.
What is the output of:
d = {}
d["x"] = d.get("x", 10) + 5
print(d)KeyError
{'x': 5}
{'x': 15}
{'x': 10}
"x" is missing, so get() returns 10, and 10 + 5 = 15.
What is the output of:
d = {"a": 1, "b": 2}
print(d.pop("z", "none"))None
{'a': 1, 'b': 2}
KeyError
none
pop() with a default returns the default when the key is missing.
Which method returns all the key-value pairs of a dictionary?
keys()
pairs()
items()
values()
items() gives (key, value) pairs.
Which method adds the pairs of one dictionary into another?
append()
insert()
update()
extend()
d1.update(d2) adds/changes pairs from d2 in d1.
What is the output of:
d = {"a": [1, 2]}
d["a"].append(3)
print(d){'a': [1, 2, 3]}
{'a': [1, 2]}
Error
{'a': 3}
Values can be any type, including lists, and the list can be changed.
What is the output of:
d = {(1, 2): "point"}
print(d[(1, 2)])KeyError
point
(1, 2)
TypeError
A tuple is immutable, so it can be used as a key.
What is the output of:
print(type({}))<class 'set'>
<class 'tuple'>
<class 'dict'>
<class 'list'>
Empty curly brackets create an empty dictionary.
Which statement adds a new pair with key "city" and value "Pune" to dictionary s?
s.insert("city", "Pune")
s.add("city": "Pune")
s.append("city", "Pune")
s["city"] = "Pune"
Assigning to a new key adds the pair.