GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python map() function - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python map() function

Last Updated : 26 Jun, 2026

map() function in Python applies a function to every element of one or more iterables and returns a map object (iterator) containing the transformed results. It is commonly used for element-wise operations and can often replace explicit loops with shorter and more readable code.

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

Output
[1, 2, 3, 4]

Explanation: map() applies int() to each element in 's' which changes their datatype from string to int.

Note: map() returns a lazy iterator, which means values are generated only when needed. Once a map object is consumed, it cannot be reused without creating a new one.

Syntax

map(function, iterable,...)

Parameters:

  • function: The function to apply to every element of the iterable.
  • iterable: One or more iterable objects (list, tuple, etc.) whose elements will be processed.

Note: You can pass multiple iterables if the function accepts multiple arguments.

Converting map object to a list

By default, map() function returns a map object. In many cases, we may need to convert this iterator to a list to work with the results directly.

Example: Let's see how to double each element of the given list.

Python
def double(val):
    return val * 2

a = [1, 2, 3, 4]    
res = list(map(double, a))
print(res)

Output
[2, 4, 6, 8]

Explanation:

  • map(double, a) applies double() to each element in 'a'.
  • list() converts the map object to a list.

map() with lambda

Lambda function can be used with map() to define short transformation logic without creating a separate function.

Python
a = [1, 2, 3, 4]
res = list(map(lambda x: x ** 2, a))
print(res)

Output
[1, 4, 9, 16]

Explanation: lambda x: x ** 2 squares each number and the results are converted into a list.

map() with multiple iterables

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

Output
[5, 7, 9]

Using map() with Tuples

map() works with any iterable, including tuples. The following example increments each element of a tuple by 1.

Python
nums = (1, 2, 3)
res = tuple(map(lambda x: x + 1, nums))
print(res)

Output
(2, 3, 4)

Explanation:

  • lambda x: x + 1 increments each element by 1.
  • map() applies this transformation to every element in the tuple nums.
  • tuple() converts the resulting map object back into a tuple.

String Manipulation with map()

Converting strings to Uppercase

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

Output
['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.

Extracting first character from strings

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

Output
['a', 'b', 'c']

Explanation: lambda s: s[0] extracts first character from each string in the list words. map() applies this lambda function to every element, resulting map object is converted to a list of the first characters using list().

Removing whitespaces from strings

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

Output
['hello', 'world', 'python']

Explanation: str.strip method removes leading and trailing whitespaces from each string in list strings and map() applies str.strip() to each element.

Real World Example

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

Output
[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.
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.