Recall & Review
beginner
What Python function can you use to pair elements from two sequences to create a dictionary?
You can use the
zip() function to pair elements from two sequences, then pass it to dict() to create a dictionary.Click to reveal answer
beginner
Given two lists
keys = ['a', 'b', 'c'] and values = [1, 2, 3], how do you create a dictionary from them?Use
dict(zip(keys, values)) to create {'a': 1, 'b': 2, 'c': 3}.Click to reveal answer
intermediate
What happens if the two sequences have different lengths when creating a dictionary with
dict(zip(keys, values))?The
zip() function stops at the shortest sequence, so the dictionary will only include pairs up to that length.Click to reveal answer
beginner
Can you create a dictionary from two sequences if one is a tuple and the other is a list?
Yes!
zip() works with any iterable sequences like lists, tuples, or strings.Click to reveal answer
beginner
Why is creating a dictionary from two sequences useful in programming?
It helps organize related data, like pairing names with phone numbers, making it easy to look up values by keys.
Click to reveal answer
Which Python function pairs elements from two sequences to create key-value pairs?
✗ Incorrect
zip() pairs elements from two sequences into tuples.What does
dict(zip(['x', 'y'], [10, 20, 30])) produce?✗ Incorrect
zip() stops at the shortest sequence, so the extra 30 is ignored.If you have
keys = ('a', 'b') and values = [1, 2], how do you create a dictionary?✗ Incorrect
Use
dict(zip(keys, values)) to pair and convert to dictionary.What type of object does
zip() return in Python 3?✗ Incorrect
zip() returns an iterator that produces tuples.Why might you prefer using
dict(zip(keys, values)) over a for-loop to create a dictionary?✗ Incorrect
Using
dict(zip()) is a clean, simple way to create dictionaries.Explain how to create a dictionary from two sequences in Python.
Think about pairing elements and converting pairs into a dictionary.
You got /4 concepts.
What happens if the sequences used to create a dictionary have different lengths? How does Python handle this?
Consider how zip pairs elements one by one.
You got /3 concepts.