The map() function is a built-in python function that allows you to manipulate items in an iterable without a for loop. This is also known as mapping. An iterable is an object that can return its values(members) one at a time, allowing it to be iterated over. The python functions list, tuple, dict (dictionary) and set can be used to create iterable objects.
Following is a basic syntax of the map function.
function – Mandatory – The function to be executed upon the object
iterables – Mandatory – iterator object (list, dict etc…, Can send any number of iterables but each iterable must hold at least a single parameter)
list_b =[9,8,7,6,5]# Function to Calculate two listsdefcalculateValues(list_one_value, list_two_value):
total = list_one_value + list_two_value
returnint(total)# Use the map function to calculate each item in the lists
In the above example, it shows how the map function can be used to easily calculate two lists using a predefined function (calculateValues) without having to resort to a for loop. This makes the code easier to read and understand. Mapping is an integral part of the functional programming style.
Link between Python Mapping function and Functional Programming
Functional programming is also known as FP for short. It is a paradigm or style of writing programs that value immutability, modularity, referential transparency, and pure functions. Functional programming evolved from lambda calculus, a mathematical system built around function abstraction and generalization. It also avoids the concepts of shared state, mutable data, which are a core part of object-oriented programming.
Functional programming mainly uses iterable data sets with a combination of various functions. The most used techniques for manipulating or transforming data are.
This consists of transforming data which is obtained through an iterable object and producing a new item(iterable) by calling a transformation on each item on the iterable object.
Filtering out items in an iterable objects to create a new item(iterable)
This applies a reduction function to an iterable object that will produce a single cumulative value.
We will be looking exclusively at Mapping Function in this tutorial.
Basic of map() Function
Simplest explanation of the map() function is that it applies a defined function to all the items in a specified iterable. Let us dig into the two main arguments of the map() function.
Function - map(function, iterables)
The first argument function is a transformation function. Which means this function will apply a transformation to each item in an iterable object and return a new item. This function can be any user function or any inbuilt python callable such as built-in functions, classes, methods, and lambda functions. The function must only be called by name without using the round brackets() even if additional arguments are required.
Iterables- map(function, iterables)
The second argument of the map() function is an object to be transformed by the defined function. This is an iterable such as list, array, dictionary, etc. If multiple iterable arguments are passed, the defined function must be capable of taking all the iterables as arguments and apply the desired transformations to all iterables. However, in a case where multiple iterables with different lengths are passed, the map() function will stop when the shorted iterable is completed.
In the following example, we pass a list of numbers with a user defined function called multiply. Multiply function multiplies the given value by five. The map() function will apply this to all the items in the list.
number = number *5returnint(number)
multiplied_list =map(multiply, numbers_list)print(list(multiplied_list))
[5, 10, 15, 20, 25]
By using map() function, it eliminates the need to use a “for loop”. Let us look at the same function with a for loop.
multiplied_list =for x in numbers_list:
x = x *5
[5, 10, 15, 20, 25]
By using map() function we can achieve the following advantages
Make the code more concise and easier to comprehend
Ability to write unit tests and debug the code easily as there is a clear differentiation between all the functions.
Increased reusability of the code as the multiply function can be reused anywhere in the program.
Less resource consumption, as any kind of loop can be a drain on system resources while a highly optimized built in function like map() is much more efficient.
These are the basics that we are trying to achieve using a functional programming style.
Using python's built-in functions using a single iterable object
Functions to transform a list of integers
Let's have a look at three functions that transform intergers into differnt numbers.
hex() - converts the given value to hexadecimal value
float() - converts the given number to decimal value
bin() - converts the given number to binary
# Numbers List
numbers_list =[-30,-10,20,35,60,101]# Convert to Hexadecimal
hex_value =map(hex, numbers_list)print(list(hex_value))# Convert to Decimal
decimal_value =map(float, numbers_list)print(list(decimal_value))#Convert to Binary
binary_value =map(bin, numbers_list)print(list(binary_value))
Functions to transform a list of strings
This time, we are looking at couple of functions that transform stings.
len() - calculate the length of each string
hash() - get the hash value of each string (can be used regardless of the data type)
# String List
fruits_list =['apple','orange','mango','pineapple','watermelon']# Get length of each item
string_length =map(len, fruits_list)print(list(string_length))# Get a reversed list
string_reversed =map(hash, fruits_list)print(list(string_reversed))
In map() we can use a lambda function as the first argument.
Lambda to multiply the given value by 5
numbers_list =[1,2,3,4,5,6]# Lambda function as first argument (multiply by 5)
multiplied_values =map(lambda num: num *5, numbers_list)print(list(multiplied_values))
[5, 10, 15, 20, 25, 30]
Using python's built-in functions using a multiple iterable objects
When using multiple iterables, the function passed to the map() function must be able to receive a number of arguments equal to the number of iterables, and the map() function will stop with the end of the shortest iterable.
pow() - get the value of x to the power of y. For each item in listone to the power of each corresponding value in listtwo. Ex: listone value = 2 to the power of list2 value = 5. (25=32)
max() - get the max of the two given values
list_two =[3,5,6,8,9]# Get the power value
power_value =map(pow, list_one, list_two)print(list(power_value))# Get the Max value of the given two values from the lists
max_value =map(max, list_one, list_two)print(list(max_value))
[1, 32, 729, 65536, 1953125]
[3, 5, 6, 8, 9]
Manipulating multiple iterable string objects using a user defined function
The _combinestring _function combines the values from each list and returns a joint string (joint greeting)
# String List
list_two =['Jack','Mary','Sammy']# Function to combine two stringsdefcombinestring(greeting, name):
full_greeting = greeting +" "+ name
greeting_list =map(combinestring, list_one, list_two)print(list(greeting_list))
When working with iterable string objects, using the map() function can be useful in easily achieving the desired outcome. The following are some examples of string manipulation using the map() function.
Using methods of str object
The python str class contains a myriad of ways to manipulate strings such as str.capitalize(), str.lower(), str.swapcase(), str.title(), and str.upper(). These are methods that do not take any additional arguments. These can be implemented, as shown in the below examples.
# String List
string_list =['new york','england','paris','kuala lumpur','tokyo']# Capitalizeprint(list(map(str.capitalize, string_list)))# Lowercaseprint(list(map(str.lower, string_list)))# Change Caseprint(list(map(str.swapcase, string_list)))# Title Caseprint(list(map(str.title, string_list)))# Uppercaseprint(list(map(str.upper, string_list)))
Also, we can use methods in string class that take additional arguments such as str.strip(). In these instances, if you directly call the method without any additional arguments, the str.strip() function will take the default value of char. In this state, the function will remove all the white spaces from the string. If you need to change the default, you can use a lambda function to provide the argument.
Using str.strip() with default char value
# String List
string_list_spaces =[' new york','england ',' paris',' kuala lumpur',' tokyo ']# Strip all Spacesprint(list(map(str.strip, string_list_spaces)))
The map() function can be used to perform transformations on numerical values such as maths and arithmetic operations, converting string values to integer or floating values. The following samples will help us understand how these transformations are achieved.
Using Math Operators
Using a user defined function called multiply_multi, we will multiple each item in an iterable by 5 and 10 and return the values
# Numbers List
number_list =[2,5,7,9,15]# Function to multiply each value by 5 and 10defmultiply_multi(number):
multiplication_one = number *5
multiplication_two = number *10return multiplication_one, multiplication_two
# Pass each item to multiply_multi function
combined_list =map(multiply_multi, number_list)print(list(combined_list))
In addition to user defined functions we can use the python Maths module and associated methods with map(). Below example provides how map() can be used with arithmetic methods sqrt() and factorial().
sqrt() - this calculate the square root of any given number
factorial() - multiple the given number from all the whole numbers down to 1 to obtain the factorial value (Example : 4! => 4 X 3 X 2 X 1 = 24)
#Import Math Moduleimport math
number_list =[1,2,4,5,6,9,10]# Find the Square Root
squre_root =map(math.sqrt, number_list)print(list(squre_root))# Find the Factorial
factorial =map(math.factorial, number_list)print(list(factorial))
Using map() function to convert between units of measure
We can use map() to convert values between multiple units of measure. We will look at the following example of converting between different units.
When creating a user-defined function to convert Kilograms to Pounds and vice versa, we assume that 1kg is equal to 2.2.20462262185 pounds(lbs) and 1lbs is equal to 0.45359237 kilograms(kg).
# Function to convert Kg to Poundsdefkgtopounds(value):
value = value /0.45359237return value
# Function to convert Pounds to Kgdefpoundstokg(value):
value = value *0.45359237return value
values_to_convert =[2,4,5,10,35]# Assuming the values in list are in Kilograms (Kg)
converted =map(kgtopounds, values_to_convert)print(list(converted))# Assuming the values in list are in Pounds (lbs)
converted =map(poundstokg, values_to_convert)print(list(converted))
Kilograms to Pounds =>
[4.409245243697551, 8.818490487395103, 11.023113109243878, 22.046226218487757, 77.16179176470715]
Pounds to Kilograms =>
[0.90718474, 1.81436948, 2.2679618500000003, 4.535923700000001, 15.875732950000002]
Using map() function to convert string values to float and integer respectively
By using the inbuilt int() and float() methods we can easily convert string items to integers and floats respectively. The main consideration before using a conversion is to verify that the items in the iterable objects are values that can be converted.
integer_string_list =['14','8','55','-34','2']# Convert to Floatprint(list(map(float, float_string_list)))# Convert to Integerprint(list(map(int, integer_string_list)))
To Float =>
[12.55, 9.0, 45.0, -16.65, 5.99]
To Integer =>
[14, 8, 55, -34, 2]
In the next example, we will send an unclean set of data to be converted, which will cause an error. The error occurs because we are trying to convert text values “Five” and “Twenty” to an integer. As the integer method does not recognize words, it throws a value error.
ValueError Traceback (most recent call last)
<ipython-input-56-0fcebcf58a9a> in <module>
4 # Unclean Data
----> 5 print(list(map(int, unclean_data)))
ValueError: invalid literal for int() with base 10: 'Five'
The map() function with other types of iterable objects
In this section, we will look at how the map() function behaves with other types of iterable objects such as Tuples and Dictionaries. Any supported iterable type can be used in conjunction with any supported function objects.
Using map() with Tuple
Tuple is a finite ordered list of items. In python triple objects are defined by items separated by commas and enclosed in round brackets
integer_tuple =(1,2,3,4,5)# Convert String to UPPERCASE
uppercase_list =map(str.upper, string_tuple)print(list(uppercase_list))# Add 5 to each item in the tuple using a lambda function
calculated_list =map(lambda x: x +5, integer_tuple)print(list(calculated_list))
Converted to String =>
['APPLE', 'ORANGE', 'PINEAPPLE', 'BANANA', 'GRAPE']
Added 5 to each item =>
[6, 7, 8, 9, 10]
Using map() with Dictionary
Dictionaries in python are a list of unordered items that are used to create a map of unique keys to values. Each key is separated from its value by a colon, items separated by commas inside curly brackets. In the below example, we check if the values in the list of the fruits are present in the dictionary keys and prints the value associated with each key.
fruits_list =['apple','orange','pineapple','banana','grape','apple']# Match Values of fruits list with fruits dictionary
match_list =list(map(fruits_dictionary.get, fruits_list))print(match_list)
Matched Result =>
['fruit1', 'fruit2', None, 'fruit3', None, 'fruit1']
As we can see from all the above examples, the map() function or mapping in python is a versatile function that can be used in various situations. Mapping can be used simply as a for loop replacement or a foundational element in writing programs in function style.