Sitemap
3 min readJun 7, 2022

--

Hello. I am one of the participants of the SistersLab’s Women in Tech Academy which is supported by the organization “Toplum Gönüllüleri Vakfı” (https://www.tog.org.tr/en/). The project aims to empower 25 women with software training and support them to participate in the workforce in the IT sector. I am very happy for getting a chance to be a part of this team. You can find more information about the project here: https://sisterslab.co/women-in-tech-academy/

Python- Lambda(), Filter() and Map() Functions

The Lambda function is an anonymous function, meaning that the function is without a name.

The lambda keyword is used to define the anonymous function in Python similar to the def keyword used for defining a normal function.

So, we can express this code →

in this format (with lambda function) →

As we see, lambda function lets us to create small, single-use function, and it saves time and space in our code.

In this example, we can read the first line as ‘ multiply x with 2 and return the result’;

We can define max function and use an if-else statement like;

Or we can write the same code as;

.

The only thing we need to keep in mind is that x comes before “if x>y” contrary to expectations in the second code.

Here, we created a new_list function in the third line. However, the type of it is ‘function’(check out the lines 5 and 10).
In order to convert it to a list, we need to use the list() function as in line 6.

In this code, we can sort each sublist as:

The sorted() function returns a sorted list of the specified iterable object.

Lambda functions can be used along with built-in functions such as filter() or map().

Using lambda() function with filter()

Get Sevda Sanver’s stories in your inbox

Join Medium for free to get updates from this writer.

Filter() function, combined with a lambda function, filters out all the elements of a sequence “sequence”, for which the function returns True.

In other words, filter() forms a new list that contains only elements that satisfy a certain condition, i.e. the function we passed returns True.

In this example, we printed the odd numbers of ‘my_list’ by using lambda function with filter:

Using lambda() function with map()

The map () function iterates through all items in the given iterable and executes the function we passed as an argument on each of them. Or we can define map() function as: the map() function is called with a lambda function and a list and a new list is returned which contains all the lambda modified items returned by that function for each item.

This code prints TRUE for even numbers, FALSE for odd numbers:

Let’s see some examples of filter and map functions to understand their concepts.

Define a mixed type list and find the integers in the list by using filter():

Let’s multiply the first element of each lists with 3 and return it:

We can capitalize the strings we have in our list with .capitalize() function or we can use .upper() function to print them in capital letters.

Let’s find the numbers bigger than 20 in our list. Then use map() function and divide them to 10 and get integer results. (Line 2 and 3 express the same thing.)

To sum up the differences of filter() and map() functions, we can say that;

  • If we would like to do a mathematical operation with every single element of a list and then create a new list with new results → we use map.
  • If we have a criteria + check the elements of the list for that criteria, then make a new list with the ones which meets the criteria → we use filter.

- If we would like to check a condition is true or false, we use filter.
-If we would like to see some of the outputs(not all), we use filter.

This was all for this week. We learned lambda, filter and map functions today. See you next time!

Press enter or click to view image in full size

--

--