Question:
Use anonymous and filter functions and print a list of numbers divisible by 13 from the list [79, 65, 54, 39, 555, 339,623,]
Program:
1 2 3 4 5 6 7 |
my_list = [79, 65, 54, 39, 555, 339,623,] # use anonymous function to filter result = list(filter(lambda x: (x % 13 == 0), my_list)) # display the result print "Numbers divisible by 13 are",result |
Explanation:
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().
Output:
1 |
Numbers divisible by 13 are [65, 39] |
Leave a Reply