0
0
Pythonprogramming~20 mins

Dictionary creation in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Dictionary Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of dictionary comprehension with condition
What is the output of this Python code?
Python
result = {x: x**2 for x in range(5) if x % 2 == 0}
print(result)
A{0: 0, 2: 4, 4: 16}
B{1: 1, 3: 9}
C{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
DSyntaxError
Attempts:
2 left
๐Ÿ’ก Hint
Look at the condition after the for loop in the comprehension.
โ“ Predict Output
intermediate
2:00remaining
Output of dictionary from zip function
What does this code print?
Python
keys = ['a', 'b', 'c']
values = [1, 2, 3]
result = dict(zip(keys, values))
print(result)
A{'a': 1, 'b': 2, 'c': 3}
B{1: 'a', 2: 'b', 3: 'c'}
CTypeError
D{('a', 1), ('b', 2), ('c', 3)}
Attempts:
2 left
๐Ÿ’ก Hint
zip pairs elements from two lists into tuples.
โ“ Predict Output
advanced
2:30remaining
Output of nested dictionary comprehension
What is the output of this code?
Python
result = {x: {y: x*y for y in range(3)} for x in range(2)}
print(result)
ASyntaxError
B{0: {0: 0, 1: 1, 2: 2}, 1: {0: 0, 1: 1, 2: 2}}
C{0: {0: 0, 1: 0, 2: 0}, 1: {0: 1, 1: 1, 2: 1}}
D{0: {0: 0, 1: 0, 2: 0}, 1: {0: 0, 1: 1, 2: 2}}
Attempts:
2 left
๐Ÿ’ก Hint
The inner dictionary multiplies x by y for y in 0 to 2.
โ“ Predict Output
advanced
2:30remaining
Output of dictionary creation with duplicate keys
What is the output of this code?
Python
result = {x % 3: x for x in range(6)}
print(result)
A{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5}
B{0: 0, 1: 1, 2: 2}
C{0: 3, 1: 4, 2: 5}
DKeyError
Attempts:
2 left
๐Ÿ’ก Hint
Keys repeat because of modulo operation; later values overwrite earlier ones.
๐Ÿง  Conceptual
expert
2:00remaining
Error raised by invalid dictionary creation syntax
What error does this code raise?
Python
result = {x: x*2 if x > 2 for x in range(5)}
ATypeError
BSyntaxError
CKeyError
DNo error, outputs {3: 6, 4: 8}
Attempts:
2 left
๐Ÿ’ก Hint
Check the placement of the if condition in the dictionary comprehension.