+

map in Python

Applies the specified function to each element of the specified sequence / sequences.

map (func, iterable, ...)

-> + py3.0 iterator | list

func - Function to be applied to elements of a sequence or sequences. It must accept the number of elements equal to the number of sequences. Before -py3.0, if None is passed, it is considered that an identity mapping (lambda * args: args) is required, and if multiple sequences are passed, the result will contain tuples with data from each of them.

iterable - - The sequence (or object that supports iteration) to which you want to apply the function. The iterator stops when the shortest of the sequences has been exhausted. Prior to -py3.0, if any sequence has fewer elements than the others, the missing elements are considered None. 


  def func (el1, el2): 
return `% s |% s`% ( el1, el2)

# python 2
map (func, [1, 2], [3, 4, 5]) # [`1 | 3`,` 2 | 4`, `None | 5`]

# python 3
list (map (func, [1, 2], [3, 4, 5])) # [`1 | 3`,` 2 | 4` ]


# python 2
dict (map (Non e, [1, 2], [3, 4])) # {1: 3, 2: 4}

# python 3
dict (map (lambda * args: args, [1, 2], [3, 4])) # {1: 3, 2: 4}

Notes

For cases where the sequence already contains ready-made tuples of arguments, use itertools.starmap().

Get Solution for free from DataCamp guru