Python Practice › Iterator & Generator Programs › Program 16
Iterators from zip and enumerate
Write a Python program that shows zip and enumerate return iterators.
Output
('a', 1)
[('b', 2), ('c', 3)]
[(1, 'x'), (2, 'y')] []Approach
- Call
next()on them and see the rest continue.
Solution
pairs = zip(["a", "b", "c"], [1, 2, 3])print(next(pairs))print(list(pairs))numbered = enumerate(["x", "y"], start=1)print(list(numbered), list(numbered))Output
('a', 1)
[('b', 2), ('c', 3)]
[(1, 'x'), (2, 'y')] []Learn the concept
New to this topic? Read the lesson, then come back.