Table of contents
1.
Introduction
2.
What is a map in Python?
3.
Python map() Function Syntax
3.1.
Python
4.
map() with Lambda
4.1.
Python
5.
Examples of map() function 
5.1.
Example 1: Converting Strings to Integers
5.2.
Python
5.3.
Example 2: Squaring Numbers
5.4.
Python
5.5.
Example 3: Lengths of Words
5.6.
Python
5.7.
Example 4: Formatting Strings
5.8.
Python
6.
Frequently Asked Questions
6.1.
Is map in Python lazy?
6.2.
What is the use of map()?
6.3.
Is Python map a generator?
7.
Conclusion
Last Updated: Dec 22, 2024
Easy

Python Map() Function

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Python is a versatile language, loved by coders worldwide for its simplicity & power. Among its many features, the map() function stands out for its utility in data processing. This function applies a given operation to each item in an iterable, such as a list, and returns a map object (which is an iterator). 

Python Map

For students diving into the world of coding, understanding map() is a stepping stone to more efficient & readable code. We'll explore its syntax, see it in action through examples, & grasp its role in making code cleaner and more Pythonic. Ready to get started? Let's unravel the magic of map() together.

What is a map in Python?

In Python, map() is a built-in function that applies a given function to each item in an iterable (like a list, tuple, or set) and returns a map object (an iterator) containing the results. The primary purpose of map() is to transform data efficiently without explicitly using loops.

Python map() Function Syntax

The map() function in Python is straightforward to use. Its basic syntax looks like this:

map(function, iterable, ...)
  • function: This is the function that you want to apply to each item in the iterable.
     
  • iterable: This can be a list, tuple, or any other object that supports iteration.

The beauty of map() lies in its simplicity. You provide a function that performs some operation, and map() applies this function to each element in the iterable, one by one. The result is a map object, which is an iterator. To see the results directly, you can convert this map object into a list or other iterable types.

Let's see an example to make this clearer:

  • Python

Python

# Define a simple function to double a number
def double(x):
return x * 2

# A list of numbers
numbers = [1, 2, 3, 4, 5]

# Use map() to apply the 'double' function to each item in 'numbers'
result = map(double, numbers)

# Convert the map object to a list to see the results
result_list = list(result)

print(result_list)
You can also try this code with Online Python Compiler
Run Code

Output

[2, 4, 6, 8, 10]


In this example, we defined a function called double that takes a single argument x and returns x * 2. We then created a list of numbers and used map() to apply the double function to each number in the list. Finally, we converted the map object to a list to see the doubled numbers.

map() with Lambda

map() becomes even more powerful when combined with lambda functions, allowing you to define the function inline:

  • Python

Python

# A list of numbers
numbers = [1, 2, 3, 4, 5]

# Use map() with a lambda function to double each number
doubled_numbers = map(lambda x: x * 2, numbers)

# Convert the map object to a list to see the results
doubled_list = list(doubled_numbers)

print(doubled_list)

You can also try this code with Online Python Compiler
Run Code

Output

2,4,6,8,10


In this example, the lambda function lambda x: x * 2 is used to double each number in the numbers list. Using lambda functions with map() can make your code more concise and readable, especially for simple operations.

Examples of map() function 

To make our understanding of the map() function more better, we will look into some practical examples. These will show how map() can be applied in different scenarios, making your code more efficient and clean.

Example 1: Converting Strings to Integers

Imagine you have a list of numeric strings that you need to convert into integers. Instead of looping through the list and converting each string individually, you can use map():

  • Python

Python

# List of numeric strings
numeric_strings = ['1', '2', '3', '4', '5']

# Use map() to convert each string to an integer
integers = map(int, numeric_strings)

# Convert the map object to a list to see the results
integers_list = list(integers)

print(integers_list)
You can also try this code with Online Python Compiler
Run Code

Output

[1, 2, 3, 4, 5]


In this example, map() takes the built-in int function and applies it to each item in the numeric_strings list, effectively converting each string to an integer.

Example 2: Squaring Numbers

Let's say you want to square each number in a list. Here's how you can do it using map():

  • Python

Python

# A function to square a number
def square(x):
return x ** 2

# A list of numbers
numbers = [1, 2, 3, 4, 5]

# Use map() to apply the 'square' function to each item in 'numbers'
squared_numbers = map(square, numbers)

# Convert the map object to a list to see the results
squared_list = list(squared_numbers)

print(squared_list)
You can also try this code with Online Python Compiler
Run Code

Output

[1, 4, 9, 16, 25]


This example demonstrates how a custom function (square) can be applied to each item in a list (numbers), producing a new list of squared numbers.

Example 3: Lengths of Words

Suppose you have a list of words and you want to find the length of each word. Instead of writing a loop, you can achieve this with a single line of code using map():

  • Python

Python

# A list of words
words = ["apple", "banana", "cherry", "dates"]

# Use map() to find the length of each word
lengths = map(len, words)

# Convert the map object to a list to see the results
lengths_list = list(lengths)

print(lengths_list)
You can also try this code with Online Python Compiler
Run Code

Output

[5, 6, 6, 5]


In this example, the built-in len function is passed to map() along with the list of words. map() applies len to each word, resulting in a list of word lengths.

Example 4: Formatting Strings

Let's say you have a list of names and you want to format them so that each name is capitalized. You can use map() with a lambda function to achieve this:

  • Python

Python

# A list of names
names = ["john", "jane", "DOE", "alice"]

# Use map() with a lambda function to capitalize each name
capitalized_names = map(lambda name: name.capitalize(), names)

# Convert the map object to a list to see the results
capitalized_list = list(capitalized_names)

print(capitalized_list)
You can also try this code with Online Python Compiler
Run Code

Output

['John', 'Jane', 'Doe', 'Alice']


Here, a lambda function is used to capitalize each name in the list. The capitalize() method is applied to each element of the names list, resulting in a new list where each name is properly capitalized.

Frequently Asked Questions

Is map in Python lazy?

Yes, map() in Python is lazy, meaning it only computes values when iterated over, avoiding immediate computation for all items.

What is the use of map()?

map() applies a given function to each item in an iterable, transforming the data efficiently without needing explicit loops.

Is Python map a generator?

Yes, Python's map() returns an iterator (like a generator), which yields items on demand instead of storing all results in memory.

Conclusion

In this article, we have learned about the map() function in Python, an invaluable tool for applying a specific operation to each item in an iterable, such as a list or tuple. We started with understanding its syntax: map(function, iterable, ...), which is both simple and powerful. With the help of  practical examples, we saw how map() can be used to transform data efficiently, from converting strings to integers and squaring numbers to finding the lengths of words and formatting strings.

Live masterclass