I am a beginner and I have to implement a code to display only even numbers from 23 to 97 using map function. I got stuck at
def evenfunc(num):
if num%2 == 0:
return num
print map(evenfunc, range(23,98))
The output is [None, 24, None, 26, None, 28, None, 30, None, 32, None, 34, None, 36,....97] So how to get rid of the none values?
Your problem is that you misunderstand what the function that's passed into
map
should do. The function passed intomap
should modify the existing input.map
maps the result of the function to each element, creating a new iterable. Not attempt to filter it.You need to use
filter
instead, which is made to specifically filter input based upon a condition:However, a list comprehension would be a better choice: