Open In App

Lambda and filter in Python Examples

Last Updated : 09 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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
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
result = list(filter(lambda x: (Counter(str) == Counter(x)), my_list)) 
  
# printing the result
print(result) 


Output :

['geeks', 'keegs']


Similar Reads

Intersection of two arrays in Python ( Lambda expression and filter function )
Given two arrays, find their intersection. Examples: Input: arr1[] = [1, 3, 4, 5, 7] arr2[] = [2, 3, 5, 6] Output: Intersection : [3, 5] We have existing solution for this problem please refer Intersection of two arrays link. We will solve this problem quickly in python using Lambda expression and filter() function. Implementation: C/C++ Code # Fun
1 min read
Spatial Filters - Averaging filter and Median filter in Image Processing
Spatial Filtering technique is used directly on pixels of an image. Mask is usually considered to be added in size so that it has a specific center pixel. This mask is moved on the image such that the center of the mask traverses all image pixels.In this article, we are going to cover the following topics - To write a program in Python to implement
3 min read
Python: Iterating With Python Lambda
In Python, the lambda function is an anonymous function. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to iterate with lambda in python. Syntax: lambda variable : expression Where, variable is used in the expressionexpression can be an mathematical expressio
2 min read
Map function and Lambda expression in Python to replace characters
Given a string S, c1 and c2. Replace character c1 with c2 and c2 with c1. Examples: Input : str = 'grrksfoegrrks' c1 = e, c2 = r Output : geeksforgeeks Input : str = 'ratul' c1 = t, c2 = h Output : rahul We have an existing solution for this problem in C++. Please refer to Replace a character c1 with c2 and c2 with c1 in a string S. We can solve th
2 min read
Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space. Examples: Input : [1, 2, 3, 2, 3, 1, 3] Output : 3 We have existing solution for this problem please refer Find the Number Occurring Odd Number of Times link. we will solv
1 min read
Difference between List comprehension and Lambda in Python
List comprehension is an elegant way to define and create a list in Python. We can create lists just like mathematical statements and in one line only. The syntax of list comprehension is easier to grasp. A list comprehension generally consists of these parts : Output expression,Input sequence,A variable representing a member of the input sequence
3 min read
Lambda expression in Python to rearrange positive and negative numbers
Given an array of positive and negative numbers, arrange them such that all negative integers appear before all the positive integers in the array. The order of appearance should be maintained. Examples: Input : arr[] = [12, 11, -13, -5, 6, -7, 5, -3, -6] Output : arr[] = [-13, -5, -7, -3, -6, 12, 11, 6, 5]\Input : arr[] = [-12, 11, 0, -5, 6, -7, 5
2 min read
Ways to sort list of dictionaries by values in Python - Using lambda function
In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge to sort dictionaries according to their values can
2 min read
Overuse of lambda expressions in Python
What are lambda expressions? A lambda expression is a special syntax to create functions without names. These functions are called lambda functions. These lambda functions can have any number of arguments but only one expression along with an implicit return statement. Lambda expressions return function objects. For Example consider the lambda expr
8 min read
Using lambda in GUI programs in Python
Python Lambda Functions are anonymous function means that the function is without a name. In this article, we will learn to use lambda functions in Tkinter GUI. We use lambda functions to write our code more efficiently and to use one function many times by passing different arguments. We use the lambda function to pass arguments to other functions
3 min read
Practice Tags :