0
0
Pythonprogramming~20 mins

Creating dictionary from two sequences in Python - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Dictionary Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2: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)
A{'a': 1, 'b': 2, 'c': 3}
B{'a': 1, 'b': 2}
C{'a': 1, 'b': 2, 'c': 3, 'd': 4}
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
Think about how zip pairs elements from two lists.
โ“ Predict Output
intermediate
2: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)
A{'x': 10, 'y': 20, 'z': 0, 'w': 0}
B{'x': 10, 'y': 20, 'z': None, 'w': None}
CIndexError
D{'x': 10, 'y': 20}
Attempts:
2 left
๐Ÿ’ก Hint
zip stops when the shortest input is exhausted.
โ“ Predict Output
advanced
2: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)
A{'a': 1, 'b': 2, 'c': 3}
BTypeError
C{'a': 2, 'b': 4, 'c': 6}
D{'a': 2, 'b': 4}
Attempts:
2 left
๐Ÿ’ก Hint
Look at how values are transformed inside the comprehension.
โ“ Predict Output
advanced
2: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)
AKeyError
B{'a': 3, 'b': 2}
CValueError
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
What happens if keys repeat in a dictionary?
๐Ÿง  Conceptual
expert
2: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))
A4
B5
C3
DIndexError
Attempts:
2 left
๐Ÿ’ก Hint
zip stops at the shortest sequence length.