0

foo = list(range(1,10)) result = list(filter(lambda x:x/2 ==1,foo)) print (result)

The results is [2], but how?

17th Apr 2020, 9:56 AM
YA Noor
YA Noor - avatar
3 Respuestas
+ 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
17th Apr 2020, 10:12 AM
Justus
Justus - avatar
+ 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]
17th Apr 2020, 12:44 PM
Lothar
Lothar - avatar
+ 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 /
17th Apr 2020, 10:11 AM
XXX
XXX - avatar