Last Updated : 14 Aug, 2025
map() function in Python is used to apply a specific function to each element of an iterable (like a list, tuple, or set) and returns a map object (which is an iterator).
Syntaxmap(function, iterable,..)
Parameters:
Basic ExampleNote: You can pass multiple iterables if the function accepts multiple arguments.
Let's start with a simple example of using map() to convert a list of strings into a list of integers.
Python
s = ['1', '2', '3', '4']
res = map(int, s)
print(list(res))
Explanation: map() applies int() to each element in s which changes their datatype from char to int.
Converting map object to a listBy default, map() function returns a map object, which is an iterator. In many cases, we will need to convert this iterator to a list to work with the results directly.
Example: Let's see how to double each elements of the given list.
Python
a = [1, 2, 3, 4]
# Custom function to double a number
def double(val):
return val * 2
res = list(map(double, a))
print(res)
Explanation:
We can use a lambda function instead of a custom function with map() to make code shorter and easier. Let's see how to improve above code for better readability.
Python
a = [1, 2, 3, 4]
res = list(map(lambda x: x * 2, a))
print(res)
Explanation: lambda x: x * 2 double each value in list a. The result was mapped and converted into a list for easy display.
map() with multiple iterablesWe can use map() with multiple iterables if the function we are applying takes more than one argument.
Example: In this example, map() takes two iterables (a and b) and applies lambda function to add corresponding elements from both lists.
Python
a = [1, 2, 3]
b = [4, 5, 6]
res = map(lambda x, y: x + y, a, b)
print(list(res))
Explanation: map() takes x from a and y from b and adds them.
Converting strings to UppercaseThis example shows how we can use map() to convert a list of strings to uppercase.
Python
fruits = ['apple', 'banana', 'cherry']
res = map(str.upper, fruits)
print(list(res))
['APPLE', 'BANANA', 'CHERRY']
Explanation: str.upper method is applied to each element in the list fruits using map(). The result is a list of uppercase versions of each fruit name.
In this example, we use map() to extract the first character from each string in a list.
Python
words = ['apple', 'banana', 'cherry']
res = map(lambda s: s[0], words)
print(list(res))
Explanation: lambda s: s[0] extracts first character from each string in the list words. map() applies this lambda function to every element, resulting in a list of the first characters of each word.
Removing whitespaces from stringsIn this example, We can use map() to remove leading and trailing whitespaces from each string in a list.
Python
s = [' hello ', ' world ', ' python ']
res = map(str.strip, s)
print(list(res))
['hello', 'world', 'python']
Explanation: str.strip method removes leading and trailing whitespaces from each string in list strings. map() function applies str.strip to each element and returning a list of trimmed strings.
Calculate fahrenheit from celsiusIn this example, we use map() to convert a list of temperatures from Celsius to Fahrenheit.
Python
celsius = [0, 20, 37, 100]
fahrenheit = map(lambda c: (c * 9/5) + 32, celsius)
print(list(fahrenheit))
[32.0, 68.0, 98.6, 212.0]
Explanation: lambda c: (c * 9/5) + 32 converts each Celsius temperature to Fahrenheit using standard formula. map() function applies this transformation to all items in the list celsius.
When to Use map()RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4