O'Reilly logo

Python Recipes Handbook: A Problem-Solution Approach by Joey Bernard

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Joey Bernard 2016

Joey Bernard, Python Recipes Handbook, 10.1007/978-1-4842-0241-8_3

3. Iterators and Generators

Joey Bernard

(1)Fredericton, New Brunswick, Canada

Often you’ll need to process some sequence of data from one source or another. The way to do this in Python is to use iterators. Many of the data types available in standard Python include an iterable interface that you can use. For those that don't, you can create a generator that then provides an iterable interface.

3-1. Iterating Over the Contents of a List

Problem

You want to iterate over the contents of a list.

Solution

While a list is iterable, you need to use the iter function to get access to the associated iterator.

How It Works

Listing 3-1 shows how to get access to the associated ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required