map and filter takes function as input

map and filter takes function as input.

map and filter takes function as input, you can define one if it has more than couple lines using def, if the function is short and simple, typically use lambda function.
example:
1
#even element from list
2
def even(N):
3
if N%2==0:
4
return True
5
else:
6
return False
7
​
8
a=[1,2,3,4,5,6]
9
list(filter(even,a))
Copied!
1
#square each list element
2
def square(N):
3
return N*N
4
a=[1,2,3,4,5,6]
5
list(map(square,a))
Copied!
Last modified 1yr ago
Copy link