Tag Archives: extract and filter content

How to extract and filter the content of an iterator in Python

Let’s say that you have an iterator and you need to list all the containing elements within it. In order to do that you can use the enumerate() function which will take an iterable object and return a list of tuples (count + value).

>>> bikes = ['Honda', 'Yamaha', 'Suzuki']
>>> bike_enum = enumerate(bikes)
>>> next(bike_enum)
(0, 'Honda')
>>> next(bike_enum)
(1, 'Yamaha')
>>> next(bike_enum)
(2, 'Suzuki')

Continue reading