Today I would like to discuss the map function in Python. How to use it, why I usually prefer using it instead of a regular For loops, and introducing list comprehensions.
To make sure I get my point across I will cover a few basics first. Hopefully, this post will also be approachable to people with little Python experience.
Iterables can be different types of ordered lists, in general, we call those lists that contain information in a set order — “Arrays”.
The map function executes a specified function for each item in an array.
Returns a list of the results after applying the given function to each item of a given iterable (more detailed explinations about iterables).
For loops are basic constructs in programming that run a piece of code repeatedly while certain conditions are met. Most programming languages contain one or more types of those looping constructs so this is familiar to anyone who knows a different programming language.
for item in list:
Keywords being for and in Followed by a colon. With the next line containing the code you want to run indented. Failing to adhere to the syntax will result in errors and frustration.
In layman terms, list comprehension is a one-line for loop for the specific task of creating a new array. Many programmers prefer list comprehensions over regular for loops because it’s shorter. However, shorter isn’t always more readable. Especially when adding a conditional statement. Overall, I find list comprehensions especially useful when I don’t need to apply more than one function or a conditional. It’s efficient and I highly recommend people to experiment with those more.
new_list = [ expression for item in items if condition ]
I’ve created a list of numbers and a function that multiplies a number times 3. Using the numbers list, I want to create a new list where each number is a number from the original list times 3.
In all the examples, we iterate over every number in the original numbers list, multiply the numbers times 3 and save it in a new list*.
*I want to note that the map function returns an “object”, not a list. If you’re unsure about the different variable types in python I’d recommend clicking here. To get the desired output I used the list function. Typically, the map function is used alongside other functions (e.g filter, reduce, and lambda functions). Unfortunately, I’m not going to explain the filter and reduce functions in this post.
(lambda <variables>: <function>)
In the above examples, I also used the lambda function. Lambda also called an “anonymous function”, is a nameless, user-defined function. Just like regular functions, lambdas can contain any number of arguments. However, you’ll want to keep your lambdas short to maintain readability. The above example may be overly simplistic, however, the lambda function is a strong tool in a programmer’s toolbox. I highly recommend new programmers to do their research before they dismiss it (why are python lambdas useful).
I often find myself drawn to the map function instead of for loops because I find it shorter and more readable.
In many cases when you’re trying to incorporate several functions inside a for loop the code can get more complicated. The map function along with other functions helps keep things simpler while not having to settle about readability. You can introduce the map function inside a for loop to avoid nested structures and additional syntax errors due to faulty indentation. Additionally, the map function helps you protect the original data in case you need to use it again in your code.
Sadly, I have not found a comprehensive study that compares for loops and map function’s performance (please comment below if you have!). Although, from different tests people performed, it seems for loops are slightly more efficient than the map function. However, both of them are very inefficient in comparison to vectorization.
At the end of the day, programming is similar to writing a story. It’s common to see two different people conveying the same message using different words and expressions. Despite map function not being as efficient as for loops in terms of computer performance, it is a tool worth mastering. I often use it to avoid nested structures in my code as the difference in performance is negligible.
Python - Variable Types
Variables are nothing but reserved memory locations to store values. This means that when you create a variable you…
Iterators are everywhere in Python. They are elegantly implemented within for loops, comprehensions, generators etc…
Python Lambda Function - The Python Guru
(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing…