MAP: map(func,iterator)
squares=list(map(lambda x:x*x,range(1,11)))
InputList=list(map(int,input().split()))
addTwoIters=list(map(lambda x,y:x+y,range(1,10),range(11,20)))
convertIntoList=list(map(list,["abc","123","xyz"]))
cubes=list(map(pow,range(10),[3]*10))
REDUCE: reduce(func,sequence)
import functools #for reduce
import operator #only for operator
l=[10,2,1,2]
Sum of first 10 numbers : print(functools.reduce(lambda x,y:x+y,range(10)))
Maximum element : print(functools.reduce(lambda x,y:x if x>y else y,l))
Multiplication of all elements : print(functools.reduce(operator.mul,l))
Concatenate elements : print(functools.reduce(operator.add,["dh","ee","raj"]))
Divide 1000 by all list elements: print(functools.reduce(lambda x,y:x//y,l,1000))
FILTER: filter(func or None, sequence)
l=[10,2,1,2]
Get Even numbers: print(list(filter(lambda x:x%2==0,l)))
l=["bib","nest","naman"]
Get palindrome strings from list(l) of strings: print(list(filter(lambda x:x==x[::-1],l)))
Remove elements with None type: print(list(filter(None, [1, 45, "", 6, 50, 0, {}, False])))
l=["bib","nest","naman","hi","bye","ta","ta"]
Print strings with length 2 : print(list(filter(lambda x:len(x)==2,l)))
l="bibnestnamanhibyetata"
Remove vowels from a string: print(''.join((filter(lambda x: x not in ['a','e','i','o','u'], l))))
REDUCE: reduce(func,sequence)
import functools #for reduce
import operator #only for operator
l=[10,2,1,2]
Sum of first 10 numbers : print(functools.reduce(lambda x,y:x+y,range(10)))
Maximum element : print(functools.reduce(lambda x,y:x if x>y else y,l))
Multiplication of all elements : print(functools.reduce(operator.mul,l))
Concatenate elements : print(functools.reduce(operator.add,["dh","ee","raj"]))
Divide 1000 by all list elements: print(functools.reduce(lambda x,y:x//y,l,1000))
FILTER: filter(func or None, sequence)
l=[10,2,1,2]
Get Even numbers: print(list(filter(lambda x:x%2==0,l)))
l=["bib","nest","naman"]
Get palindrome strings from list(l) of strings: print(list(filter(lambda x:x==x[::-1],l)))
Remove elements with None type: print(list(filter(None, [1, 45, "", 6, 50, 0, {}, False])))
l=["bib","nest","naman","hi","bye","ta","ta"]
Print strings with length 2 : print(list(filter(lambda x:len(x)==2,l)))
l="bibnestnamanhibyetata"
Remove vowels from a string: print(''.join((filter(lambda x: x not in ['a','e','i','o','u'], l))))
Comments
Post a Comment