Python Map Function
When it comes to using a functional style in Python to process data, three techniques are used most commonly- mapping, filtering, and reducing. As per Guido van Rossum, compared to functional languages, imperative programming languages strongly influence Python. However, in 1993, the community demanded some functional programming features, including anonymous functions, a map () function, reduce () function, and a filter () function.
These three map filters reduce python functional features that were added to the Python programming language, and today, these are considered the fundamental components of functional programming in Python. In this tutorial blog, we will talk about what the python map function is, how to use it, and the difference between the map function and the filter function.
What is Map Function in Python?
Thinking about what is a map in python? It is a python function that works as an iterator to process and transforms every item in an iterable (lists, tuples, arrays, etc.) without using an explicit or loop. This technique is known as mapping and is used when you want to apply a single transformation to every item in an iterable and return an iterator that results.
In simple words, map () is the function that helps in transforming every single original item into a new (transformed) item. Both function and iterable are passed as arguments to the map () function in Python.
Syntax
The below-given is the syntax of the python map function:
map(function, iterable)
In this syntax:
- The function is basically a transformation function through which every single item of the iterable is to be passed.
- Iterable is a sequence or collection, including a list, array, tuple, etc., that is to be mapped.
This example will help you better understand the syntax of the Map function in Python.
The below-given code creates a list of numbers, and then the map () function is used to multiply each item of the iterable with itself.
# Defining a function
def mul(i):
return i * i
# Using the map function
x = map(mul, (3, 5, 7, 11, 13))
print (x)
print(list(x))
Here is the output:
<map object at 0x7fc5a85e1ca0>
[9, 25, 49, 121, 169]
Workings of the Map () Function
The Map () function in Python is known to take a function and the iterable/s. It loops over all items of an iterable, and then the transformation function is applied to the items. After passing the map function, it returns a map object storing the value of each transformed item. The input function can be in-built functions, user-defined functions, classes, lambda functions, or methods. Now that you know what a Map () function and its workings are, let’s learn the use of the map function in python with built-in functions, lambda functions, and as an iterator.
Using Map in Python with Built-In Functions
The map () function in Python itself is a built-in function and can also be used with other pre-defined built-in functions.
Let’s understand how the map () function can be used with the built-in functions with the help of examples.
Example: Using Map () with Round ()
In this code, the round () function is used along with the map () function to round off values for each item in the list.
my_list = [2.6743,3.63526,4.2325,5.9687967,6.3265,7.6988,8.232,9.6907]
updated_list = map(round, my_list)
print(updated_list)
print(list(updated_list))
Here is the output:
<map object at 0x000000E65F901748>
[3, 4, 4, 6, 6, 8, 8, 10]
Example: Using Map () with Math. Sqrt ()
In this code, the math library is first imported to use the math—sqrt () function along with the map () function.
Import math
num = [9, 36, 49, 81, 121]
x = list(map(math. sqrt, num))
print(x)
Here is the output:
[3.0, 6.0, 7.0, 9.0, 11.0]
Using Map in Python with Lambda Functions
The map () function is commonly used with the lambda functions. Lambda functions in Python are known as anonymous functions because these do not have any names. With the help of this particular function, you can define the function directly inside the map () function.
Let’s understand how the map () function can be used with the lambda functions with the help of examples.
Example:
The lambda function inside the map () function is used to multiply each value in the list with 10.
my_list = [2,3,4,5,6,7,8,9]
updated_list = map(lambda x: x * 10, my_list)
print(updated_list)
print(list(updated_list))
Here is the output:
<map object at 0x000000BD18B11898>
[20, 30, 40, 50, 60, 70, 80, 90]
Using Map in Python as an Iterator
The map () function in Python can be used with a string and in such a case, the latter will act as an array. Then this function can be used as an iterator to iterate through every single string character.
Let’s understand how the map () function can be used as an iterator with the help of an example
Example: Using myMapFunc () with Map () Function
The function myMapFunc () is used with the map () function to convert the given string to uppercase after passing it to the former.
def myMapFunc(s):
return s.upper()
my_str = “welcome to the tutorial!”
updated_list = map(myMapFunc, my_str)
print(updated_list)
for i in updated_list:
print(i, end=””)
Here is the output:
<map object at 0x000000DF2E711748>
WELCOME TO THE TUTORIAL!
Map vs. Filter: Learning the Difference
Both Map and Filter are among the most confusing functions in Python, but they are also one of the most important ones.
To learn the difference between map and filter in python, let’s take a look at an example:
Map and Filter
list(map(lambda var: var*2, range(0,10)))
The output is:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
list(filter(lambda var: var%2, == 0, range(0,10)))
The output is:
[0, 2, 4, 6, 8]
Map ()
The basic application of the map () function is to manipulate iterables. This particular function executes all its conditions on each item in the iterable.
For instance, in the above given example, each element in the range 0-10 is multiplied by 2, giving a new list of elements. All elements of an iterable are taken into account by the map function, enabling you to apply a function on it and pass it to the output with the same or different values.
Filter ()
As the name suggests, Python’s filter () function is used to filter the iterables according to the conditions. The filter function filters the original iterable and then passes the items that return True for a specific function provided to the filter (). Therefore, when it comes to the output, you can expect to see only the items present in the iterable.
For instance, in the above example, the condition is applied as a lambda function, and the elements satisfying the conditions are present in the list. The output displays the elements that are divisible by 2, and the remaining elements are filtered out.
Key Takeaway
Summarizing the article, here are some key points we’ve learned about the python map function.
- Map () function is a built-in function that processes and transforms every item in an iterable without using an explicit or loop.
- The syntax of the map () function is – map(function, iterable).
- In the syntax, the function is a transformation function through which all items in the iterable need to pass.
- In the syntax, iterable is a sequence or collection that is to be mapped.
- The map function can be used with built-in functions, lambda functions, and as an iterator.
Looking forward to learning more about Map Function in Python? Explore our blog or take a course to learn Python programming language!