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

product

itertools.product generates a Cartesian product (a set of all permutations), a concise equivalent of a nested for loop. Here is how it appears. First, we create three sets (product can take any number of iterables):

s1 = {'Peter', 'Benjamin'}s2 = {'Flopsy', 'Mopsy', 'Cottontail'}s3 = {'McGregor', 'Thomas', 'Bea'}

Next, we call a product function on all of them, printing the result:

>>> from itertools import product>>> for el in product(s1, s2, s3):        print(el)('Peter', 'Mopsy', 'Bea')('Peter', 'Mopsy', 'Thomas')('Peter', 'Mopsy', 'McGregor')('Peter', 'Cottontail', 'Bea')('Peter', 'Cottontail', 'Thomas')('Peter', 'Cottontail', 'McGregor')('Peter', 'Flopsy', 'Bea')('Peter', 'Flopsy', 'Thomas')('Peter', 'Flopsy', 'McGregor')('Benjamin', ...
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