Python map() Function with EXAMPLES

โšก Smart Summary

Python map() applies a given function to every item of an iterable such as a list, tuple, set, dictionary, or string. It returns a lazy map object that you convert with list() or iterate using a loop.

  • ๐Ÿ”ข Syntax: map() takes a function plus one or more iterables and applies that function to each element.
  • ๐Ÿ” Return value: map() returns a lazy map object, so wrap it in list(), tuple(), or set() to view the results.
  • ๐Ÿงฉ Iterables: Lists, tuples, sets, dictionaries, and strings all work as inputs to map().
  • โšก Lambda: Pairing map() with a lambda avoids defining a separate named function for simple transformations.
  • โž• Multiple iterables: Passing several iterables applies the function across them in parallel, stopping at the shortest.
  • ๐Ÿค– AI assistance: AI assistants such as GitHub Copilot generate map() calls, and map-style transforms feed data pipelines in machine learning.

Python map() Function

Syntax

map(function, iterator1,iterator2 ...iteratorN)

Parameters

Here are two important parameters:

  • function: A mandatory function to be given to map(), which will be applied to all the items available in the iterator.
  • iterator: A compulsory iterable object. It can be a list, a tuple, etc. You can pass multiple iterator objects to the map() function.

Return Value

The map() function applies the given function on all the items inside the iterator and returns an iterable map object, i.e., a tuple, a list, etc.

How map() function works?

The map() function takes two inputs: a function and an iterable object. The function given to map() is a normal function, and it iterates over all the values present in the iterable object.

For example, consider you have a list of numbers, and you want to find the square of each of the numbers.

To get the output, we need a function that will return the square of the given number. The function is as follows:

def square(n):
	return n*n

The list of items for which we want to find the square is as follows:

my_list = [2,3,4,5,6,7,8,9]

Now let us use the map() built-in function to get the square of each of the items in my_list.

The final code is as follows:

def square(n):
    return n*n
my_list = [2,3,4,5,6,7,8,9]
updated_list = map(square, my_list)
print(updated_list)
print(list(updated_list))

Output:

<map object at 0x0000002C59601748>
[4, 9, 16, 25, 36, 49, 64, 81]

The output of the map() function, as seen above, is a map object displayed on the screen as <map object at 0x0000002C59601748>.

You will have to iterate the output from map() using a for-loop or the list() method to get the final output. I have used list() in the code to display the values inside the given list.

So using the map() function, we are able to get the square of each number. The list given to map() was [2,3,4,5,6,7,8,9], and using the function square(), the output we got is [4, 9, 16, 25, 36, 49, 64, 81].

The map() function applies the function square() on all the items in the list. For example, it takes the my_list variable and updates the list with the square of each number. The output is stored in the updated_list variable.

Using map() with Python built-in functions

The Python map() function is a built-in function and can also be used with other built-in functions available in Python. In this example, we are going to make use of the Python round() built-in function that rounds the given values.

Example:

The list that I have is my_list = [2.6743,3.63526,4.2325,5.9687967,6.3265,7.6988,8.232,9.6907]. I need the rounded values for each item present in the list, so we will make use of round() as the function to map().

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))

Output:

<map object at 0x000000E65F901748>
[3, 4, 4, 6, 6, 8, 8, 10]

The round() function is applied to all the items in the list, and it returns a list with all values rounded, as shown in the output.

Using map() with a string as an iterator

We can also make use of map() on a string. In Python, a string acts like an array, so we can easily use it inside map().

In this example, we have a function myMapFunc() that takes care of converting the given string to uppercase. The function myMapFunc() is given to the map() function. The map() function will take care of converting the given string to uppercase by passing the string to myMapFunc().

def myMapFunc(s):
    return s.upper()
my_str = "welcome to guru99 tutorials!"
updated_list = map(myMapFunc, my_str)
print(updated_list)
for i in updated_list:
    print(i, end="")

Output:

<map object at 0x000000DF2E711748>
WELCOME TO GURU99 TUTORIALS!

Using map() with a List of Numbers

To work with a list in map(), we will take a list of numbers and multiply each number in the list by 10.

The list that we are going to use is [2,3,4,5,6,7,8,9]. The function myMapFunc() takes care of multiplying the given number by 10. The function is given to map() along with the list.

Example:

def myMapFunc(n):
    return n*10

my_list = [2,3,4,5,6,7,8,9]

updated_list = map(myMapFunc, my_list)
print(updated_list)
print(list(updated_list))

Output:

<map object at 0x000000EE2C061898>
[20, 30, 40, 50, 60, 70, 80, 90]

The output we see is that each number in the list is multiplied by 10.

Using map() with Tuple

A tuple is an object in Python that has items separated by commas and enclosed in round brackets. In this example, we will take a tuple with string values. The function that we will use will convert the given values to uppercase.

Example:

def myMapFunc(n):
    return n.upper()

my_tuple = ('php','java','python','c++','c')

updated_list = map(myMapFunc, my_tuple)
print(updated_list)
print(list(updated_list))

Output:

<map object at 0x0000009C3C3A16A0>
['PHP', 'JAVA', 'PYTHON', 'C++', 'C']

The output we get is a tuple back with all the values in it converted to uppercase.

Using map() with Dictionary

A dictionary in Python is created using curly brackets ({}). Since the dictionary is an iterator, you can make use of it inside the map() function. Let us now use a dictionary as an iterator inside map().

The following example shows the working of a dictionary iterator inside map():

def myMapFunc(n):
    return n*10
my_dict = {2,3,4,5,6,7,8,9}
finalitems = map(myMapFunc, my_dict)
print(finalitems)
print(list(finalitems))

Output:

<map object at 0x0000007EB451DEF0>
[20, 30, 40, 50, 60, 70, 80, 90]

Using map() with Set

A set in Python is an unordered collection of items in curly brackets ({}). Since set() is also an iterator, you can make use of it inside the map() function.

Here is a working example of using a set as an iterator inside map():

def myMapFunc(n):
    return n*10
my_set = {2,3,4,5,6,7,8,9}
finalitems = map(myMapFunc, my_set)
print(finalitems)
print(list(finalitems))

Output:

<map object at 0x000000AC8F05DEF0>
[20, 30, 40, 50, 60, 70, 80, 90]

Using map() with Lambda function

In Python, lambda expressions are utilized to construct anonymous functions. You will have to use the lambda keyword just as you use def to define normal functions.

So in this example, we are going to use the lambda function inside map(). The lambda function will multiply each value in the list by 10.

Example:

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))

Output:

<map object at 0x000000BD18B11898>
[20, 30, 40, 50, 60, 70, 80, 90]

Using Multiple Iterators inside map() function

Example 1: Passing two list iterators to map()

You can send more than one iterator, i.e., a list, a tuple, etc., all at the same time to the map() function.

For example, if you want to add two lists, the same can be done using the map() function. We are going to make use of two lists, my_list1 and my_list2.

In the example below, the first item in my_list1 is added to the first item of my_list2. The function myMapFunc() takes in items of my_list1 and my_list2 and returns the sum of both.

Here is the working example of adding two given lists using the map() function.

def myMapFunc(list1, list2):
    return list1+list2

my_list1 = [2,3,4,5,6,7,8,9]
my_list2 = [4,8,12,16,20,24,28]

updated_list = map(myMapFunc, my_list1,my_list2)
print(updated_list)
print(list(updated_list))

Output:

<map object at 0x0000004D5F751860>
[6, 11, 16, 21, 26, 31, 36]

Example 2: Passing one Tuple and a list iterator to map()

We are going to make use of a list and a tuple iterator in the map() function. The function given to map(), myMapFunc(), will get the items from the list and the tuple. The items will be joined with an underscore (_). The working example is shown below:

def myMapFunc(list1, tuple1):
    return list1+"_"+tuple1

my_list = ['a','b', 'b', 'd', 'e']
my_tuple = ('PHP','Java','Python','C++','C')

updated_list = map(myMapFunc, my_list,my_tuple)
print(updated_list)
print(list(updated_list))

Output:

<map object at 0x00000059F37BB4E0>
['a_PHP', 'b_Java', 'b_Python', 'd_C++', 'e_C']

FAQs

Both apply an operation to each item. A list comprehension is often more readable and returns a list directly, while map() returns a lazy map object and can be faster when reusing an existing named function.

No. map() never changes the original iterable. It reads each element, applies your function, and produces a new lazy map object, leaving the source list, tuple, or string untouched.

In Python 3, map() is lazy: it returns a map object that computes values only when iterated. This saves memory on large data. Wrap it in list() or loop over it to get results.

map() transforms every item, filter() keeps only items passing a test, and reduce() (from functools) combines all items into a single value. Together they cover transform, select, and aggregate operations.

When iterables differ in length, map() stops at the shortest one. Extra items in the longer iterables are ignored, so no error is raised and the result matches the shortest iterable.

No. A map object is an iterator that is exhausted after one pass. Once you loop through it or call list() on it, it is empty. Convert it to a list first if you need reuse.

In machine learning, map() applies preprocessing functions across datasets, such as scaling numbers, tokenizing text, or transforming features before training. It gives a clean, functional way to run the same operation over many samples.

Yes. GitHub Copilot autocompletes map() calls with the right function and iterables, and agentic AI tools can refactor loops into map() expressions, then run your tests to confirm the behavior is unchanged.

Summarize this post with: