Recall & Review
beginner
What does the
zip() function do in Python?The
zip() function takes multiple sequences (like lists or tuples) and pairs their elements together into tuples, creating an iterator of these pairs.Click to reveal answer
beginner
How does
zip() handle sequences of different lengths?It stops creating pairs when the shortest input sequence is exhausted, so the output length matches the shortest input.
Click to reveal answer
beginner
How can you convert the result of
zip() into a list?You can wrap
zip() with list(), like list(zip(seq1, seq2)), to get a list of tuples.Click to reveal answer
beginner
What will be the output of
list(zip([1, 2], ['a', 'b'], [True, False]))?The output will be
[(1, 'a', True), (2, 'b', False)]. Each tuple contains elements from each list at the same position.Click to reveal answer
intermediate
Can
zip() be used to unzip a list of tuples? How?Yes! Using the unpacking operator
*, like zip(*zipped_list), you can separate paired elements back into individual sequences.Click to reveal answer
What does
zip([1, 2, 3], ['a', 'b', 'c']) return when converted to a list?✗ Incorrect
The
zip() pairs elements by position, so it creates tuples of corresponding elements.If you zip lists of different lengths, what happens?
✗ Incorrect
The
zip() function stops at the shortest input sequence length.How do you unzip a list of tuples
zipped = [(1, 'a'), (2, 'b')]?✗ Incorrect
Using the unpacking operator
* with zip() separates the tuples back into individual sequences.What type does
zip() return?✗ Incorrect
zip() returns an iterator that produces tuples.Which of these is a correct way to pair elements from two lists
a and b?✗ Incorrect
The
zip() function takes multiple sequences as separate arguments.Explain how the
zip() function works and give an example with two lists.Think about matching socks from two drawers.
You got /3 concepts.
Describe how to unzip a list of tuples created by
zip().Imagine separating pairs back into individual items.
You got /3 concepts.