VOOZH about

URL: https://towardsdatascience.com/using-map-and-filter-in-python-ffdfa8b97520/

⇱ Using Map and Filter in Python | Towards Data Science


Skip to content

Using Map and Filter in Python

How to use the built-in map and filter functions in python

7 min read
👁 Photo by Kevin Ku on Unsplash
Photo by Kevin Ku on Unsplash

In this tutorial, we will learn when and how we would use the built-in map and filter functions in Python.


Map Function

Let’s say you want to create a list using a list that you already have. Meaning you want to use the list you have, apply some sort of operation or function to each element, and use those outputs to create a new list.

An example would be if we have a list of numbers and we wanted to create a new list that contains their squares. Well, one way to accomplish this would be to iterate over the list of numbers using a for loop and apply a function that returns the square of each number or element. And as we are iterating over the list, we can add or append the squared values to our new list.

Let’s see how we would do that in code:

We have a list of numbers, num_list, and we want to create a new list, num_list_squared, that contains the squares of num_list. We use a for loop to loop over num_list, and append the square of each number or num to our num_list_squared list.


Another way of accomplishing this would be to use the built-in python function called map.

The map function takes in two arguments: the function we want to apply and the iterable object or sequence we want to apply it to (such as a list in this case). In other words, the map function maps or applies this function to each element of the sequence that we pass in.


The format we would use for the map function is as follows:

map(function you want to apply, sequence of elements we want to apply it to)


This map function will return a map object, which is an iterator. If we want to create a list from this map object, we would need to pass in our map object to the built-in list function as follows:

list(map(function, sequence))

Let’s see how we can accomplish the above code using the built-in map function:

Remember that we can apply the map function to each element in any iterable object or sequence, not just a list.


So let’s break down what happened in this line of code:

num_list_squared = list(map(squared, num_list))

The map function took the first element from num_list, which is a 1, and passed it in as an argument to the squared function (since we passed that function in as the first argument to the map function). The squared function then returned the square of 1, which is 1, and it was added to our map object. The map function then took the second element from num_list, which is 2, and passed it in as an argument to the squared function. The squared function returned the square of 2, which is 4, which was then added to our map object. After it finished going through the num_list elements and the rest of our squared numbers were added to the map object, the list function casts this map object onto a list, and that list was assigned to the variable num_list_squared.


Using Lambda Expressions

We can shorten our code even further by instead passing in a lambda expression as our function:


If you’re not familiar with lambda expressions, you can check out this tutorial:

Lambda Expressions in Python


Note: The functions that we pass in to map can be built-in functions in python. For example, if we have a list of strings and we want to create a new list that contains the lengths of these strings, we can just pass in the built-in len function as follows:

list(map(len, list_of_strings))

To learn more about iterables, iterators, and iteration:

Iterables and Iterators in Python


Filter Function

Again, let’s say we want to create a list using a list that we already have. But this time we want our new list to contain only the elements that satisfy a given condition. For example, we have a list of numbers, and we want to create a new list that contains only the even numbers from our list. We can accomplish this task with a for loop as follows:

We have a list of numbers, list_of_nums, that contains the numbers 1, 2, 3, 4, 5, and 6. We want to create a new list of numbers, list_of_even_nums, that only contains the even numbers from list_of_nums. So we created a function, is_even, that takes in an input, and returns True if that input is even, or False if it is not. We then created a for loop that loops through list_of_nums and checks if each number of that list is even by passing in that element to the is_even function. If the is_even function returns True, that number is appended to list_of_even_nums. If is_even returns False, then that number is not appended to list_of_even_nums.


Another way of accomplishing this would be to use the built-in python function called filter. The filter function takes in two arguments: the function that checks for a specific condition and the sequence we want to apply it to (such as a list in this case). The filter function takes each element from our list and passes it in to the function we give it. If the function with that specific element as an argument returns True, the filter function will add that value to the filter object (that we can then create a list from just like we did with the map object above). If the function returns False, then that element will not be added to our filter object.

In other words, we can think of the filter function as filtering our list or sequence based on some condition.

The format we use for the filter function is as follows:

filter(function that checks for a condition, sequence of elements we want to apply it to)

This filter function will return a filter object, which is an iterator. If we want to create a list from this filter object, we would need to pass in our filter object to the built-in list function (just like we did with the map object) as follows:

list(filter(function, sequence))

Let’s now create the same list as above using the built-in filter function:

The filter function took the first element from list_of_nums, which is a 1, and passed it in as an argument to the is_even function (since we passed that function in as the first argument to the filter function). The is_even function then returns False, since 1 is not even, so 1 is not added to our filter object. The filter function then took the second element from list_of_nums, which is 2, and passed it in as an argument to the is_even function. The is_even function returns True, since 2 is even, and thus 2 is added to our filter object. After it goes through the rest of the elements in list_of_nums and the rest of the even numbers are added to our filter object, the list function casts this filter object onto a list, and that list was assigned to the variable list_of_even_nums.


Using Lambda Expressions

We can shorten our code even further by instead passing in a lambda expression as our function:


If you enjoy reading stories like these and want to support me as a writer, consider signing up to become a Medium member. It’s $5 a month, giving you unlimited access to stories on Medium. If you sign up using my link, I’ll earn a small commission.

Join Medium with my referral link – Luay Matalka


Conclusion

In this tutorial, we learned how the python built-in map and filter functions work. We also saw some examples of them being used. And lastly, we saw how lambda expressions can be passed in as arguments to these functions.


Written By

Luay Matalka

Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.

Write for TDS

Related Articles