Extract a list of numbers that are multiples of 5 from a list of integers named input_list.

Lambda and filter in Python Examples

Prerequisite : Lambda in Python

Given a list of numbers, find all numbers divisible by 13.

Input : my_list = [12, 65, 54, 39, 102, 339, 221, 50, 70] Output : [65, 39, 221]

We can use Lambda function inside the filter() built-in function to find all the numbers divisible by 13 in the list. In Python, anonymous function means that a function is without a name.

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.




# Python Program to find numbers divisible
# by thirteen from a list using anonymous
# function
# Take a list of numbers.
my_list = [12, 65, 54, 39, 102, 339, 221, 50, 70, ]
# use anonymous function to filter and comparing
# if divisible or not
result = list(filter(lambda x: (x % 13 == 0), my_list))
# printing the result
print(result)

Output:



[65, 39, 221]

Given a list of strings, find all palindromes.




# Python Program to find palindromes in
# a list of strings.
my_list = ["geeks", "geeg", "keek", "practice", "aa"]
# use anonymous function to filter palindromes.
# Please refer below article for details of reversed
# https://www.geeksforgeeks.org/reverse-string-python-5-different-ways/amp/
result = list(filter(lambda x: (x == "".join(reversed(x))), my_list))
# printing the result
print(result)

Output :

['geeg', 'keek', 'aa']

Given a list of strings and a string str, print all anagrams of str




# Python Program to find all anagrams of str in
# a list of strings.
from collections import Counter
my_list = ["geeks", "geeg", "keegs", "practice", "aa"]
str = "eegsk"
# use anonymous function to filter anagrams of x.
# Please refer below article for details of reversed
# https://www.geeksforgeeks.org/anagram-checking-python-collections-counter/amp/
result = list(filter(lambda x: (Counter(str) == Counter(x)), my_list))
# printing the result
print(result)

Output :

['geeks', 'keegs']

Extract a list of numbers that are multiples of 5 from a list of integers named input_list.




Article Tags :
Python
Python-Built-in-functions
python-lambda

Python: Filter a list of integers using Lambda

Last update on February 26 2020 08:09:21 (UTC/GMT +8 hours)

Python List Comprehension

In this article, we will learn about Python list comprehensions, and how to use it.

4. Lambda Operator, filter, reduce and map

By Bernd Klein. Last modified: 01 Feb 2022.

On this page

If Guido van Rossum, the author of the programming language Python, had got his will, this chapter would have been missing in our tutorial. In his article from May 2005 "All Things Pythonic: The fate of reduce() in Python 3000", he gives his reasons for dropping lambda, map(), filter() and reduce(). He expected resistance from the Lisp and the scheme "folks". What he didn't anticipate was the rigidity of this opposition. Enough that Guido van Rossum wrote hardly a year later: "After so many attempts to come up with an alternative for lambda, perhaps we should admit defeat. I've not had the time to follow the most recent rounds, but I propose that we keep lambda, so as to stop wasting everybody's talent and time on an impossible quest." We can see the result: lambda, map() and filter() are still part of core Python. Only reduce() had to go; it moved into the module functools.

His reasoning for dropping them is like this:

  • There is an equally powerful alternative to lambda, filter, map and reduce, i.e. list comprehension
  • List comprehension( is more evident and easier to understand
  • Having both list comprehension and "Filter, map, reduce and lambda" is transgressing the Python motto "There should be one obvious way to solve a problem"

Extract a list of numbers that are multiples of 5 from a list of integers named input_list.

Some like it, others hate it and many are afraid of the lambda operator. The lambda operator or lambda function is a way to create small anonymous functions, i.e. functions without a name. These functions are throw-away functions, i.e. they are just needed where they have been created. Lambda functions are mainly used in combination with the functions filter(), map() and reduce(). The lambda feature was added to Python due to the demand from Lisp programmers.

The general syntax of a lambda function is quite simple:

lambda argument_list: expression

The argument list consists of a comma separated list of arguments and the expression is an arithmetic expression using these arguments. You can assign the function to a variable to give it a name.

The following example of a lambda function returns the sum of its two arguments:

sum = lambda x, y : x + y sum(3,4)

OUTPUT:

7

The above example might look like a plaything for a mathematician. A formalism which turns an easy to comprehend issue into an abstract harder to grasp formalism. Above all, we could have had the same effect by just using the following conventional function definition:

def sum(x,y): return x + y sum(3,4)

OUTPUT:

7

We can assure you that the advantages of this approach will be apparent, when you will have learnt to use the map() function.