How Lambda is used with filter in python

0 votes
How Lambda() is used with filter() in python? Can you please explain with an example? Thank you..
May 20, 2019 in Python by Neel
1,056 views

1 answer to this question.

0 votes

The filter() function in Python takes in a function and a list as arguments. This offers an elegant way to filter out all the elements of a sequence “sequence”, for which the function returns True. Here is a small program that returns the odd numbers from an input list:

# Python code to illustrate 
# filter() with lambda() 
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61] 
final_list = list(filter(lambda x: (x%2 != 0) , li)) 
print(final_list) 

Output:

[5, 7, 97, 77, 23, 73, 61]

answered May 20, 2019 by Rakshi

Related Questions In Python

0 votes
1 answer

How random.seed(): is used in python?

Pseudo-random number generators work by performing some ...READ MORE

answered Aug 24, 2018 in Python by Priyaj
• 58,020 points
710 views
0 votes
1 answer

What is absolute import in Python and how is it used?

An absolute {import, path, URL} tells you exactly how ...READ MORE

answered Nov 30, 2018 in Python by Nymeria
• 3,560 points

edited Dec 10, 2018 by Nymeria 1,403 views
0 votes
1 answer

How to create a unicode string in python with the string eg: This is a string?

Hey, @Roshni, It is very simple to execute, ...READ MORE

answered Jun 23, 2020 in Python by Gitika
• 65,770 points
828 views
0 votes
1 answer

how easy is yaml to work with in python compared to json?

Hi, First thing YAML and JSON are not ...READ MORE

answered Dec 16, 2020 in Python by MD
• 95,460 points
875 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,460 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,116 views
0 votes
1 answer

How is lambda() used with map() in python?

The map() function in Python takes in ...READ MORE

answered May 20, 2019 in Python by Takeshi
832 views
0 votes
1 answer

How do I use lambda() with reduce() in python?

The reduce() function in Python takes in ...READ MORE

answered May 20, 2019 in Python by Umer
1,958 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP