# Tutorial by Examples

## Basic use of map, itertools.imap and future_builtins.map

The map function is the simplest one among Python built-ins used for functional programming. map() applies a specified function to each element in an iterable: names = ['Fred', 'Wilma', 'Barney'] Python 3.x3.0 map(len, names) # map in Python 3.x is a class; its instances are iterable # Out: &...

## Mapping each value in an iterable

For example, you can take the absolute value of each element: list(map(abs, (1, -1, 2, -2, 3, -3))) # the call to `list` is unnecessary in 2.x # Out: [1, 1, 2, 2, 3, 3] Anonymous function also support for mapping a list: map(lambda x:x*2, [1, 2, 3, 4, 5]) # Out: [2, 4, 6, 8, 10] or convert...

## Mapping values of different iterables

For example calculating the average of each i-th element of multiple iterables: def average(*args): return float(sum(args)) / len(args) # cast to float - only mandatory for python 2.x measurement1 = [100, 111, 99, 97] measurement2 = [102, 117, 91, 102] measurement3 = [104, 102, 95, 101] ...

## Transposing with Map: Using "None" as function argument (python 2.x only)

from itertools import imap from future_builtins import map as fmap # Different name to highlight differences image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] list(map(None, *image)) # Out: [(1, 4, 7), (2, 5, 8), (3, 6, 9)] list(fmap(None, *image)) # Out: [(1, 4, 7), (2, 5, 8),...

## Series and Parallel Mapping

map() is a built-in function, which means that it is available everywhere without the need to use an 'import' statement. It is available everywhere just like print() If you look at Example 5 you will see that I had to use an import statement before I could use pretty print (import pprint). Thus ppr...

Page 1 of 1