Skip to Content
Learn Python by Building Data Science Applications
book

Learn Python by Building Data Science Applications

by Philipp Kats, David Katz
August 2019
Beginner
482 pages
12h 56m
English
Packt Publishing
Content preview from Learn Python by Building Data Science Applications

cycle

itertools.cycle allows the iterable to be gone through, repeating itself multiple times, or until the certain criteria are met. For example, if you have a plot with colors assigned by a category, and there is a chance there are more categories than colors in the style guide, you can make use of cycle. Consider the following example; we first initialize the cycle object, passing categories as arguments:

from itertools import cyclecolors = cycle(('red', 'green', 'blue'))categories = (1, 2, 3, 4, 5)

Next, we loop over both iterators by using the zip function. As you know, generally, zip loops until the shortest collection ends. In this case, there are five categories but just three colors, so without cycling, the loop will only print the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python for Data Science

Python for Data Science

Yuli Vasiliev
Introduction to Machine Learning with Python

Introduction to Machine Learning with Python

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9781789535365Supplemental Content