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
mapshould do. The function passed intomapshould modify the existing input.mapmaps the result of the function to each element, creating a new iterable. Not attempt to filter it.You need to use
filterinstead, which is made to specifically filter input based upon a condition:However, a list comprehension would be a better choice: