Map() in Python | Python for Data Science | Day 13
Welcome to our tutorial on Map() in Python. This article will cover various instances to demonstrate how map() in Python can be used in various instances. The map() function has two parameters – an iterable object (to be iterated upon) and a function (to implement on the iterable object). Thus, eliminating the need for a loop.
The main reason why we use map() is simple. We just wish to apply the function to all the provided iterables. So, let’s begin by first creating a function.
This function i.e. func2() returns the uppercase of the argument passed. So, without any delay, let’s use a map() function in conjunction with it.
As explained above, the first argument is map() is the function to be used and the second argument is the iterable object. The iterable object passed in this instance is a tuple which consists of three strings. We wish to apply the function on each of these strings. So, ideally, the output should be:
[THE, DATA, MONK]
Let’s print “a” and see our output.
This is certainly not what we expected. This is because a map object can not really be printed. So, lets convert it into a list first and then compare it with the output we expected.
Alright, let’s look at another example.
Let’s define another function.
The function – func() takes two arguments and returns their sum.
Now, we will use it in a map() function.
Great. We see the sum of two tuples passed to the map() function. Let’s try the same function, but this time with tuples of strings.
So, the list is shown as displayed above.
You can also read about map() here: https://realpython.com/python-map-function/#:~:text=Python’s%20map()%20is%20a,them%20into%20a%20new%20iterable.
Map() is a fairly easy but extremely useful concept in function. So, to test the knowledge you have acquired so far, from this tutorial, we have curated a set of questions for you along with the solutions. We highly recommend that you attempt the questions below on your own without seeking help.
We have covered 40+ complete Data Science company interviews from the candidates who cracked these interviews.
Data Science Companies interview questions
We also have 30+ e-books on Amazon, Insta Mojo and books which can be delivered directly on your email address
Complete Set of e-books from The Data Monk
Understand some of the very complex topics in Analytics which are asked in most of the interviews
The Data Monk Top Articles
How to become a Data Scientist? Complete study material, free resources and websites to practice
Become a Data Scientist
Make your profile on our website and practice at least 5-7 questions per day. Be a part of ~2000 Analytics expert
Intern | The Data Monk