+ 1

Map & filter

string =[ 12,14,5,16] result = list(filter(lambda x: x+2,string)) print(result) # answer is 12,14,5,16 def add_some(x): return x+2 string =[12,14,5,16] result=list(map(add_some, string)) print ( result) # answer is 14,16,7,18 string =[ 12,14,5,16] result = list(filter(lambda x: x%2==0,string)) print(result) # answeris 12,14,16 def add_some(x): return x%2==0 string =[12,14,5,16] result=list(map(add_some, string)) print ( result) # answeris true ,true,false,true

3rd Oct 2019, 5:00 PM
Niru Niranjan
Niru Niranjan - avatar
1 Answer
0
filter is used to filter(extract) part of an object... When you do this. result = list(filter(lambda x:x%2==0, string)) It actually mean: even = [] for i,x in enumerate(result): if x%2 == 0: even.append(x) print(even) You only want the even number but map() function won't do that, it instead check for even items from the list so you get bool as a result
3rd Oct 2019, 6:16 PM
Mirielle
Mirielle - avatar