Challenge - 5 Problems
Dictionary Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
What is the output of this dictionary creation?
Given two lists, what is the resulting dictionary after running this code?
Python
keys = ['a', 'b', 'c'] values = [1, 2, 3] result = dict(zip(keys, values)) print(result)
Attempts:
2 left
๐ก Hint
Think about how zip pairs elements from two lists.
โ Incorrect
The zip function pairs elements from keys and values by position. Then dict() converts these pairs into key-value pairs in the dictionary.
โ Predict Output
intermediate2:00remaining
What happens if the sequences have different lengths?
What is the output of this code when keys and values have different lengths?
Python
keys = ['x', 'y', 'z', 'w'] values = [10, 20] result = dict(zip(keys, values)) print(result)
Attempts:
2 left
๐ก Hint
zip stops when the shortest input is exhausted.
โ Incorrect
zip stops pairing when the shortest list ends, so only first two pairs are created.
โ Predict Output
advanced2:00remaining
What is the output of this dictionary comprehension?
What does this code print?
Python
keys = ['a', 'b', 'c'] values = [1, 2, 3] result = {k: v*2 for k, v in zip(keys, values)} print(result)
Attempts:
2 left
๐ก Hint
Look at how values are transformed inside the comprehension.
โ Incorrect
Each value is multiplied by 2 before being assigned to the key.
โ Predict Output
advanced2:00remaining
What error does this code raise?
What error occurs when trying to create a dictionary from these sequences?
Python
keys = ['a', 'b', 'a'] values = [1, 2, 3] result = dict(zip(keys, values)) print(result)
Attempts:
2 left
๐ก Hint
What happens if keys repeat in a dictionary?
โ Incorrect
Duplicate keys overwrite previous values. The last value for 'a' is kept.
๐ง Conceptual
expert2:00remaining
How many items are in the dictionary created here?
Given these sequences, how many key-value pairs will the dictionary have?
Python
keys = ['k1', 'k2', 'k3', 'k4'] values = [10, 20, 30, 40, 50] result = dict(zip(keys, values)) print(len(result))
Attempts:
2 left
๐ก Hint
zip stops at the shortest sequence length.
โ Incorrect
zip pairs only four items because keys has length 4, so dictionary has 4 items.