New course! Every coder should learn Generative AI!
Try a free lesson0
>>> 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...
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])
+ 1
Then what you typed should work. I hope you aren't typing the command prompt (>>>)?
0
I am using python 2.7x