map with condtion


The conversion function can do anything. It can have a condition inside.


examples/functional/map_with_condition.py
numbers = [1, 2, 3, 4]

def cond_double(n):
   if n % 2:
      return 2*n
   else:
      return n

cd = map(cond_double, numbers)
print(cd)                        # [2, 2, 6, 4]