Menu Close

Series and Parallel Mapping (Map function) in Python…FcuktheCode

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 print). Thus print is not a built-in function

Series mapping

In this case each argument of the iterable is supplied as argument to the mapping function in ascending order. This arises when we have just one iterable to map and the mapping function requires a single argument.

Example 1

Ame = ['Naruto', 'Dragon_Ball_Z', 'OverLord', 'Your_Name']
temp_mod = lambda x: x + ' is an Anime'

print(list(map(temp_mod, Ame)))

OUTPUT:
[‘Naruto is an Anime’,  ‘Dragon_Ball_Z is an Anime’,  ‘OverLord is an Anime’,  ‘Your_Name is an Anime’]


Example 2

Ame = ['Naruto', 'Dragon_Ball_Z', 'OverLord', 'Your_Name']

print(list(map(len, Ame)))  # the len function is executed

# OUTPUT:  [6, 13, 8, 9]

 

Parallel mapping

In this case each argument of the mapping function is pulled from across all iterables (one from each iterable) in parallel.

Thus the number of iterables supplied must match the number of arguments required by the function.

Ame = ['Naruto', 'Dragon_Ball_Z', 'OverLord', 'Your_Name']

Action = ["Dragon_Ball_Z", "Naruto", "Overlord", "Parasyte"]
RomCom = ["Your_Name", "Overflow", "Pet_girl", "Eromanga"]
Thriller = ["Full_metal", "Death_Note", "Psycho-Pass", "A.O.T"]

def anime(temp, x, y, z):
    return '{0}, {1}, {2}, and {3} ARE ALL ANIME'.format(temp.title(), x, y, z)

Example 3

'''
Too many arguments
observe here that map is trying to pass one item each from each of the four iterables to len. 
This leads len to complain that it is being fed too many arguments
'''

print(list(map(len, Ame, Action, RomCom, Thriller)))

'''
Results in
TypeError: len() takes exactly one argument (4 given)
'''

Example 4

'''
Too few arguments
observe here that map is supposed to execute animal on individual elements of insects one-by-one.
But anime complain when it only gets one argument, whereas it was expecting four.
'''
print(list(map(animals, insects)))

'''
Results in
TypeError: anime() missing 3 required positional arguments: 'x', 'y', and 'z'
'''

Example 5

Ame = ['Naruto', 'Dragon_Ball_Z', 'OverLord', 'Your_Name']

Action = ["Dragon_Ball_Z", "Naruto", "Overlord", "Parasyte"]
RomCom = ["Your_Name", "Overflow", "Pet_girl", "Eromanga"]
Thriller = ["Full_metal", "Death_Note", "Psycho-Pass", "A.O.T"]

def anime(temp, x, y, z):
    return '{0}, {1}, {2}, and {3} ARE ALL ANIME'.format(temp.title(), x, y, z)

print(list(map(anime,Ame,Action,RomCom,Thriller)))

OUTPUT:
[‘Naruto,  Dragon_Ball_Z,  Your_Name,  and  Full_metal  ARE  ALL  ANIME’,  ‘Dragon_Ball_Z,  Naruto,  Overflow,  and  Death_Note  ARE  ALL  ANIME’,  ‘Overlord,  Overlord,  Pet_girl,  and  Psycho-Pass  ARE  ALL ANIME’,  ‘Your_Name,  Parasyte,  Eromanga,  and  A.O.T  ARE  ALL  ANIME’]