List comprehension - double


We take the original example where we had a function called double, and this time we write a different expression to run the function on every element of an iterable.

examples/functional/list_comprehension_double.py
def double(n):
    return 2*n

numbers = [1, 2, 3, 4]
name = "FooBar"

double_numbers = [double(n) for n in numbers]
print(double_numbers) # [2, 4, 6, 8]


double_chars = [double(n) for n in name]
print(double_chars)   # ['FF', 'oo', 'oo', 'BB', 'aa', 'rr']