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

Comprehensions

Comprehensions are a nice and expressive way to work with data structures. Let's start with a simple example:

{el**2 for el in range(3)}>>> {0, 1, 4}

Here, the curly brackets define our result. We use range to create the initial iterable, and then loop over its values, computing the square value of each. This is not a real loop, though. List comprehensions are actually faster than loops and even map, as there are no lambdas, and thus, no additional costs for stack lookups:

>>> %%timeit... s = set()... for el in range(10):...     s.add(el**2)3.35 µs ± 134 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)>>> %timeit set(map(lambda x: x**2, range(10)))3.72 µs ± 207 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) ...
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