map python 3 code example

Example 1: map in python 3

map(function, iterable, ...)

Example 2: pyhton map

def calculateSquare(n):
    return n*n


numbers = (1, 2, 3, 4)
result = map(calculateSquare, numbers)
print(result)

# converting map object to set
numbersSquare = list(result)
print(numbersSquare)

Example 3: map in python

#Syntax
map(functions, iterables)

Example 4: python map

map(function_to_apply, list_of_inputs)

Example 5: map python 3

#generate a list from map iterable with lambda expression
list( map(lambda x: x*2, numeros) )

Example 6: map python 3

from multiprocessing import Pool

def test( v ):
    print(v)
    return v

if __name__ == '__main__':
    with Pool(2) as p:
        print(p.map(test, range(5)))