0
foo = list(range(1,10)) result = list(filter(lambda x:x/2 ==1,foo)) print (result)
The results is [2], but how?
3 Answers
+ 1
The line
Result=list(filter(lambda x:x/2 == 1,foo))
for range (1,10) i.e 1 to 9
only 2 fits that condition, cos only 2/2 == 1
+ 3
You can use this filter expression with using modulo operator %:
...
result = list(filter(lambda x:x%2 ==1,foo))
...
result will be: [1, 3, 5, 7, 9]
+ 1
YA Noor because only 2 is the number that gives 1 when divided by 2.
2/2 = 1
Hint:
If you wanted to filter out the odd numbers from the list, you might wanna use % instead of /