Iterators from zip and enumerate

EasyIterator & Generator Programs · 16 of 20

Write a Python program that shows zip and enumerate return iterators.

Advertisement
Output
('a', 1)
[('b', 2), ('c', 3)]
[(1, 'x'), (2, 'y')] []

Approach

  1. Call next() on them and see the rest continue.

Solution

Python
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.

More iterator & generator programs

← All 20 Iterator & Generator Programs

Leave a Comment