From the course: Python for JavaScript Developers

Unlock the full course today

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

From JavaScript map and filter to an elegant Python comprehensions

From JavaScript map and filter to an elegant Python comprehensions - Python Tutorial

From the course: Python for JavaScript Developers

Start my 1-month free trial

From JavaScript map and filter to an elegant Python comprehensions

- [Instructor] Python has built-in map and filter functions. Most of the time though, you will see people map and filter using something called comprehension. Now, while comprehensions can be difficult to grasp at first, I think you have quite an advantage coming from JavaScript. You may really accelerate your learning using your knowledge of map and filter in JavaScript. So here, we have an array that goes from zero to four. So zero, one, two, three, four. Next, we use map to create a new array. Now, this array has each number of our original array plus one. So zero, one, two, three, four becomes one, two, three, four, five. Then, we have a filter, which allows us to produce a new array that only has numbers that are greater than two. So one, two, three, four, five becomes three, four, five. Now, suppose we wanted to do that with comprehensions. How would we go about it step by step? So we start with zero to four.…

Contents