0
0
Pythonprogramming~5 mins

Creating dictionary from two sequences in Python - Quick Revision & Summary

Choose your learning style9 modes available
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?
Azip()
Bmap()
Cfilter()
Dreduce()
What does dict(zip(['x', 'y'], [10, 20, 30])) produce?
A{'x': 10, 'y': 20, None: 30}
B{'x': 10, 'y': 20}
C{'x': 10, 'y': 20, '': 30}
DError due to length mismatch
If you have keys = ('a', 'b') and values = [1, 2], how do you create a dictionary?
Akeys + values
Bdict(keys, values)
Czip(dict(keys), dict(values))
Ddict(zip(keys, values))
What type of object does zip() return in Python 3?
AAn iterator of tuples
BA list of tuples
CA dictionary
DA tuple of lists
Why might you prefer using dict(zip(keys, values)) over a for-loop to create a dictionary?
AIt only works with lists
BIt runs slower
CIt is more concise and readable
DIt creates a list instead
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.