Given a list of N numbers, use a single list comprehension to produce a new list that only contains those values that are:
(a) even numbers, and
(b) from elements in the original list that had even indices
I am looking for a solution to the above problem. As suggested here there is an easy way to solve it but I would like to know if there is a way I can use a combination of map, lambda and filter on the "FULL" input list.
I am trying to do something like this but it doesn't work.
>>> map(lambda i,x : x%2==0 or (i+1)%2==0, range(0,len(l)-1),l)
[False, True, False, True, True]
Ideally I need to write something like (added "x if") but that doesnt work. Any suggestions?
map(lambda i,x : ***x if*** x%2==0 or (i+1)%2==0, range(0,len(l)-1),l)
mapwill not help you on this. It does not filter the input. It just applies a function to each element in the list.