Menu Close

Map module using series mapping 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 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’]



Morae Q!

  1. OS operating system module using path parameter.
  2. Find the smallest and largest integers after sorting using bubble sort.
  3. Find the integer and its number of occurrences.
  4. Algorithm complexity – Big O Notation with Examples.
  5. Linear search.
  6. Map module using series mapping and parallel mapping.
  7. Mapping and Transposing with Map Module.
  8. Map Module/built-in Function.
  9. Linux commands for managing files.
  10. Program which takes two lists and maps two lists into a dictionary.
  11. Splitting strings and substituting using regular expressions regex.
  12. Basics of regular expression Regex.
  13. Find the square value of the dictionary.
  14. Check whether the given key is present in the dictionary.
  15. Matching an expression only in specific places using regular expressions.
  16. Escaping special characters using regular expressions.
  17. Check the key.
  18. Grouping and sub-grouping using regular expressions.
  19. Generate a Dictionary that Contains Numbers in the Form (x , x*x).
  20. Algorithm complexity Big-Theta, Big-Omega and Big-O Notations.