>>> def even(X): ... if X % 2 == 0 : return True ... else: return False ... >>> filter(even, [0,1,2,3,4,5,6]) [0,2,4,6] How to get that output, help please... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

>>> def even(X): ... if X % 2 == 0 : return True ... else: return False ... >>> filter(even, [0,1,2,3,4,5,6]) [0,2,4,6] How to get that output, help please...

14th Sep 2016, 11:07 PM
Vinh Le
Vinh Le - avatar
3 Answers
+ 1
filter doesn't return a list anymore in Python 3.x, so you have to use list(). def even(X): if X % 2 == 0: return True else: return False print(list(filter(even, [0,1,2,3,4,5,6]))) You can also use: print([X for X in [0,1,2,3,4,5,6] if X % 2 == 0])
14th Sep 2016, 11:55 PM
Zen
Zen - avatar
+ 1
Then what you typed should work. I hope you aren't typing the command prompt (>>>)?
15th Sep 2016, 9:33 AM
Zen
Zen - avatar
0
I am using python 2.7x
15th Sep 2016, 12:29 AM
Vinh Le
Vinh Le - avatar