Tutorial

Ultimate Guide to Python Map Function for Data Processing

Updated on December 18, 2024
English
Ultimate Guide to Python Map Function for Data Processing

Introduction

We can use the Python built-in function map() to apply a function to each item in an iterable (like a list or dictionary) and return a new iterator for retrieving the results. map() returns a map object (an iterator), which we can use in other parts of our program. We can also pass the map object to the list() function, or another sequence type, to create an iterable.

The syntax for the map() function is as follows:

map(function, iterable, [iterable 2, iterable 3, ...])

Instead of using a for loop, the map() function provides a way of applying a function to every item in an iterable. Therefore it can often be more performant since it is only applying the function one item at a time rather than making copies of the items into another iterable. This is particularly useful when working on programs processing large data sets. map() can also take multiple iterables as arguments to the function by sending one item from each iterable to the function at a time.

In this tutorial, we’ll review three different ways of working with map(): with a lambda function, with a user-defined function, and finally with a built-in function using multiple iterable arguments.

Using a Lambda Function

The first argument to map() is a function, which we use to apply to each item. Python calls the function once for every item in the iterable we pass into map() and it returns the manipulated item within a map object. For the first function argument, we can either pass a user-defined function or we can make use of lambda functions, particularly when the expression is less complex.

The syntax of map() with a lambda function is as follows:

map(lambda item: item[] expression, iterable)

With a list like the following, we can implement a lambda function with an expression that we want to apply to each item in our list:

numbers = [10, 15, 21, 33, 42, 55]

To apply an expression against each of our numbers, we can use map() and lambda:

mapped_numbers = list(map(lambda x: x * 2 + 3, numbers))

Here we declare an item in our list as x. Then we add our expression. We pass in our list of numbers as the iterable for map().

In order to receive the results of this immediately we print a list of the map object:

print(mapped_numbers)
Output
[23, 33, 45, 69, 87, 113]

We have used list() so that the map object is returned to us as a list, rather than a less human-readable object like: <map object at 0x7fc250003a58>. The map object is an iterator over our results, so we could loop over it with for or we can use list() to turn it into a list. We’re doing this here because it’s a good way to review the results.

Ultimately map() is most useful when working with large datasets, so we would likely work with the map object further, and generally would not be using a constructor like list() on them.

For smaller datasets, list comprehensions may be more suitable, but for the purposes of this tutorial we’re using a small dataset to demonstrate map().

Implementing a User-defined Function

Similarly to a lambda we can use a function we have defined to apply to an iterable. While lambda functions are more useful to implement when you’re working with a one-line expression, user-defined functions are more appropriate when the expression grows in complexity. Furthermore, when we need to pass another piece of data to the function that you’re applying to your iterable, user-defined functions can be a better choice for readability.

For example, in the following iterable, each item is a dictionary that contains different details about each of our aquarium creatures:

aquarium_creatures = [
 {"name": "sammy", "species": "shark", "tank number": 11, "type": "fish"},
 {"name": "ashley", "species": "crab", "tank number": 25, "type": "shellfish"},
 {"name": "jo", "species": "guppy", "tank number": 18, "type": "fish"},
 {"name": "jackie", "species": "lobster", "tank number": 21, "type": "shellfish"},
 {"name": "charlie", "species": "clownfish", "tank number": 12, "type": "fish"},
 {"name": "olly", "species": "green turtle", "tank number": 34, "type": "turtle"}
]

We’ve decided that all the aquarium creatures are in fact going to move into the same tank. We need to update our records to reflect that all of our creatures are moving into tank 42. To have map() access each dictionary and each key:value pair in the dictionaries, we construct a nested function:

def assign_to_tank(aquarium_creatures, new_tank_number):
 def apply(x):
  x["tank number"] = new_tank_number
  return x
 return map(apply, aquarium_creatures)

We define an assign_to_tank() function that takes aquarium_creatures and new_tank_number as parameters. In assign_to_tank() we pass apply() as the function to map() on the final line. The assign_to_tank function will return the iterator resulting from map().

apply() takes x as an argument, which represents an item in our list — a single dictionary.

Next we define that x is the "tank number" key from aquarium_creatures and that it should store the passed in new_tank_number. We return each item after applying the new tank number.

We call assign_to_tank() with our list of dictionaries and the new tank number we want to replace for each creature:

assigned_tanks = assign_to_tank(aquarium_creatures, 42)

Once the function completes we have our map object stored in the assigned_tanks variable, which we turn into a list and print:

print(list(assigned_tanks))

We’ll receive the following output from this program:

Output
[{'name': 'sammy', 'species': 'shark', 'tank number': 42, 'type': 'fish'}, {'name': 'ashley', 'species': 'crab', 'tank number': 42, 'type': 'shellfish'}, {'name': 'jo', 'species': 'guppy', 'tank number': 42, 'type': 'fish'}, {'name': 'jackie', 'species': 'lobster', 'tank number': 42, 'type': 'shellfish'}, {'name': 'charlie', 'species': 'clownfish', 'tank number': 42, 'type': 'fish'}, {'name': 'olly', 'species': 'green turtle', 'tank number': 42, 'type': 'turtle'}]

We’ve mapped the new tank number to our list of dictionaries. Using a function that we define, we can incorporate map() to apply the function efficiently on each item of the list.

Using a Built-in Function with Multiple Iterables

In the same way as lambda functions or our own defined functions, we can use Python built-in functions with map(). To apply a function with multiple iterables, we pass in another iterable name following the first one. For example, using the pow() function that takes in two numbers to find the power of the base number to the provided exponent.

Here we have our lists of integers that we would like to use with pow():

base_numbers = [2, 4, 6, 8, 10]
powers = [1, 2, 3, 4, 5]

Next we pass in pow() as our function into map() and provide the two lists as our iterables:

numbers_powers = list(map(pow, base_numbers, powers))

print(numbers_powers)

map() will apply the pow() function to the same item in each list to provide the power. Therefore our results will show 2**1, 4**2, 6**3, and so on:

Output
[2, 16, 216, 4096, 100000]

If we were to provide map() with an iterable that was longer than the other, map() would stop calculating once it reaches the end of the shortest iterable. In the following program we’re extending base_numbers with three additional numbers:

base_numbers = [2, 4, 6, 8, 10, 12, 14, 16]
powers = [1, 2, 3, 4, 5]

numbers_powers = list(map(pow, base_numbers, powers))

print(numbers_powers)

As a result, nothing will change within the calculation of this program and so it will still yield the same result:

Output
[2, 16, 216, 4096, 100000]

We’ve used the map() function with a Python built-in function and have seen that it can handle multiple iterables. We’ve also reviewed that map() will continue to process multiple iterables until it has reached the end of the iterable with the fewest items.

Conclusion

In this tutorial, we’ve explored various methods of utilizing the map() function in Python. You now have the ability to use map() with custom functions, lambda expressions, and other built-in functions. Additionally, map() can be applied to functions that require multiple iterables, enhancing its versatility in data processing tasks.

For demonstration purposes, we converted the results from map() directly into a list. In practical applications, the map object returned can be further manipulated to suit specific needs.

To deepen your understanding of Python, please use the following resources:

These resources will provide you with a comprehensive understanding of Python’s capabilities and how to effectively utilize them in your projects.

If you would like to learn more Python, check out our How To Code in Python series and our Python topic page. To learn more about working with data sets in functional programming, check out our article on the filter() function.

FAQs

What does map() in Python do?

The map() function in Python takes a function and one or more iterables and returns an iterator that applies the given function to each element of the provided iterables. In other words, it “maps” the function across each item in the iterable. For example:

numbers = [1, 2, 3, 4]
squares = map(lambda x: x**2, numbers)

Here, squares will be an iterator of 1, 4, 9, 16.

How do you create a map in Python?

You create a map object by calling the built-in map() function with a function and at least one iterable as arguments. For example:

def add_one(x):
    return x + 1

my_list = [1, 2, 3]
mapped = map(add_one, my_list)  # Creates a map object

You can then iterate over mapped or convert it to a list to see the results:

print(list(mapped))  # [2, 3, 4]

Is map lazy in Python?

Yes, in Python 3, map() returns a lazy iterator, meaning it does not process or store all results in memory at once. Instead, it computes each result on demand as you iterate over it. This can be more memory-efficient, especially for large datasets, but it also means you cannot directly index or repeatedly iterate over the same map object without reconstructing it.

How does the map() function work?

The map() function works as follows:

  1. You provide a function and one or more iterables.
  2. map() retrieves an element from each iterable.
  3. It calls the function with these elements as arguments.
  4. It yields the result of that function call.
  5. It repeats this process until any of the iterables is exhausted.

If multiple iterables are given, map() stops when the shortest iterable is exhausted. For example:

numbers = [1, 2, 3]
others = [10, 20, 30]
result = map(lambda x, y: x + y, numbers, others)
print(list(result))  # [11, 22, 33]

Should I use map in Python?

Whether you should use map() depends on personal preference and readability:

Pros:

  • It can be more concise in some cases.
  • It can be slightly faster than list comprehensions in certain scenarios (though often not significantly).

Cons:

  • Code using list comprehensions or generator expressions is often considered more “Pythonic” and more easily readable.
  • Newer Python programmers may find list comprehensions more intuitive.

In short, use map() if it makes your code clearer and more direct. Otherwise, list comprehensions or generator expressions are a very common alternative.

How to convert map to string in Python?

A map object is an iterator, not a string. If you want to convert the results of a map() call into a string, you first need to iterate over it. Common approaches include:

  • Converting to a list and then to a string representation:
mapped = map(str, [1, 2, 3])
string_representation = str(list(mapped))  # "[‘1’, ‘2’, ‘3’]"
  • Joining the results if they are string elements:
mapped = map(str, [1, 2, 3])
joined_string = ''.join(mapped)  # "123"

The best method depends on whether you want a human-readable list representation (str(list(...))) or a concatenation of the results (''.join(...)).

What does map count() do?

map objects in Python do not have a built-in count() method. The count() method is available on lists, strings, and certain other collections. If you want to count occurrences of a value produced by a map object, you must first convert it to a list (which consumes the iterator):

mapped = map(lambda x: x*2, [1, 2, 3, 2])
mapped_list = list(mapped)
count_of_4 = mapped_list.count(4)  # 2, because 2*2=4 appears twice

If you need a count without converting to a list, you could iterate manually:

count_of_value = sum(1 for x in map(lambda x: x*2, [1, 2, 3, 2]) if x == 4)

What do map and filter do in Python?

  • map(function, iterable): Applies function to each element of iterable and returns an iterator of the results.

  • filter(function, iterable): Returns an iterator of elements from iterable for which function(element) is True. If function is None, it returns elements that are truthy by themselves.

For example:

nums = [1, 2, 3, 4, 5]
mapped_nums = map(lambda x: x*2, nums)        # [2, 4, 6, 8, 10]
filtered_nums = filter(lambda x: x > 2, nums) # [3, 4, 5]

map transforms each element, while filter selects certain elements based on a condition.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Default avatar

Sr Technical Writer

Senior Technical Writer @ DigitalOcean | 2x Medium Top Writers | 2 Million+ monthly views & 34K Subscribers | Ex Cloud Consultant @ AMEX | Ex SRE(DevOps) @ NUTANIX


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.