Map, Filter, and Lambda in Python-Complete Tutorial

map, filter and lambda in Python

This Map, Filter, and Lambda in Python-Complete Tutorial is made for you. At the end of this tutorial, you will have full knowledge of the Map, Filter, and Lambda in Python.

Hello, & Welcome!

Part 17- Map, Filter, and Lambda in Python-Complete Tutorial

In this article of Python Tutorial, you will learn following-

  1. Map in Python.
  2. Filter in Python.
  3. Lambda in Python

Map in Python.

A map() function is a way to make a list, apply some kind of function on each data item within a list. And to change the item and create a new list without updating items within it.

Syntax of a map() function-

map(function,data)

Here, a function performs some operation. And data is a list that we want to update.

Let’s understand with the help of an example-

Here, in the example, we have to jumble or shuffle the words.

from random import shuffle
def jumble(word):
   anagram=list(word)
   shuffle(anagram)
   return ".join(anagram)
words=["Beetroot","Carrots","Potatoes"]
anagram=[]
print(list(map(jumble,words)))

Here, in “from random import shuffle” shuffle is a by-default package.

In “print(list(map(jumble,words)))”, a list typecasts into a list. A jumble is a function. And words is data or list.

Filter in Python.

A Filter() method is an easy way to take a collection or list and filter some values based on function result and this gives a new filter collection.

Syntax of Filter() method-

filter(function,list)

Suppose there is a grade list, consist of different grades-

grades=[‘A’,’B’,’F’,’C’,’F’,’A’]

but inside this list, we don’t want to show less grade, so to remove less grade we need to filter the list.

For example from grades=[‘A’, ‘B’, ‘F’, ‘C’, ‘F’, ‘A’] we want to remove ‘F’ grade, so for this task, we need to create a function which removes ‘F’.

grades=['A','B','F','C','F','A']
def remove_fails(grade):
    return grades!= 'F'
print(list(filter(remove_fails,grades)))

OUTPUT-
['A','B','C','A']

Here, in “print(list(filter(remove_fails, grades)))” a list typecasts into the list. “remove_fails” is a function which removes ‘F’ and grades is a list in which we wanna apply filter.

Lambda in Python

A lambda in python is an anonymous function, which means it doesn’t require any names. It is used when there is a need of function which is used only once, so at that time, you can use lambda. A Lambda is used once.

Syntax of Lambda function-

lambda arguments : statements

A lambda function can take more than one number of arguments.

Suppose, you want to square numbers in a list, then you can use a lambda function for this task.

Let’s understand with the help of an example-

nums=[1,2,3,4,5,6]
print(list(map(lambda n:n*n,nums)))

OUTPUT-
[1,4,9,16,25,36]

Here, a lambda function is used, which performs a square operation.

That’s all for the Map, Filter, and Lambda in Python. I hope now you have a better understanding of the Map, Filter, and Lambda in Python.

Congratulations! You successfully learned the Map, Filter, and Lambda in Python.

Map, Filter, and Lambda in Python

In the next tutorial, we will start learning Decorators in Python.

Till then, Enjoy Learning Python!

Are you looking for Best Books on Python for Beginners and for Intermediate/Experts?

Check these Books, These Books are the best selling and covered everything in detail.

I hope you will find these Books helpful-

Best Selling Python Books for Beginners

Best Selling Python Books for Intermediates/Experts

All the Best!

Back to Python Tutorial

Thank YOU!

Though of the Day…

“Live as if you were to die tomorrow. Learn as if you were to live forever.” 

Mahatma Gandhi

author image

Written By Aqsa Zafar

Founder of MLTUT, Machine Learning Ph.D. scholar at Dayananda Sagar University. Research on social media depression detection. Create tutorials on ML and data science for diverse applications. Passionate about sharing knowledge through website and social media.

Leave a Comment

Your email address will not be published. Required fields are marked *