From the course: Functional Programming with Python

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

List comprehensions in Python

List comprehensions in Python - Python Tutorial

From the course: Functional Programming with Python

Start my 1-month free trial

List comprehensions in Python

- [Instructor] Once you've gotten the hang of working with lists using the math and filter functions, you may be interested to discover that Python provides another way to do pretty much the same thing and that's by using something called list comprehensions. Put simply, list comprehensions are a nice readable way to both filter and transform the elements in a list. I usually think of them as a sort of combination of the three concepts we learned previously: map, filter and lambdas. So what do list comprehensions look like and how can we use them? Well, to answer this question, let's imagine, that as in the previous videos, we have a list of numbers that we want to transform in some way, whether it's by changing it to the elements by doubling them or by getting some subset of the elements, such as getting all the even numbers. So here's the basic syntax of using list comprehensions to do this. When we want to…

Contents