Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
101 views
in Python by (176k points)
retagged by
How do you use lambda functions in Python?

Please log in or register to answer this question.

1 Answer

0 votes
by (176k points)

In Python, lambda functions are often used in combination with built-in functions like filter(), map(), and reduce() to create concise and readable code.

Here are some examples of how to use lambda functions in Python:

  1. Using lambda with filter():

The filter() function returns an iterator that contains the elements from an iterable (such as a list) that meet a certain condition. You can use a lambda function to define the condition that elements must meet in order to be included in the iterator.

my_list = [1, 2, 3, 4, 5, 6]
filtered_list = list(filter(lambda x: x % 2 == 0, my_list))
print(filtered_list)  # Output: [2, 4, 6]
 

In this example, we define a lambda function that takes one argument x and checks if it is even (x % 2 == 0). We then use this lambda function with the filter() function to create a new list that contains only the even numbers from my_list.

  1. Using lambda with map():

The map() function returns an iterator that applies a function to each element of an iterable (such as a list). You can use a lambda function to define the operation that the map() function will perform on each element.

my_list = [1, 2, 3, 4, 5, 6]
mapped_list = list(map(lambda x: x * 2, my_list))
print(mapped_list)  # Output: [2, 4, 6, 8, 10, 12]
 

In this example, we define a lambda function that takes one argument x and multiplies it by 2 (x * 2). We then use this lambda function with the map() function to create a new list that contains each element of my_list multiplied by 2.

  1. Using lambda with reduce():

The reduce() function is used to apply a function to a sequence of elements, producing a single value as the output. You can use a lambda function to define the operation that reduce() will perform on each element.

from functools import reduce

my_list = [1, 2, 3, 4, 5, 6]
sum = reduce(lambda x, y: x + y, my_list)
print(sum)  # Output: 21
 

In this example, we define a lambda function that takes two arguments x and y and returns their sum (x + y). We then use this lambda function with the reduce() function to sum all the elements of my_list. The reduce() function applies the lambda function to the first two elements of the list, then applies it to the result and the next element, and so on, until all the elements have been processed and a single value (the sum of all the elements) is returned.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Mar 27, 2023 in Python by kvdevika (176k points)
0 votes
2 answers
asked Mar 27, 2023 in Python by kvdevika (176k points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...