• Python

Command Palette

Search for a command to run...

Native Functions
  • Python Abs Function
  • Python Any All Functions
  • Python Zip Function
  • Python Sum Function
  • Python Filter Function
  • Python Max Min Functions
  • Python Map Function
  • Python Round Function
  • Python Sorted Function
Data Types
  • Python Integers
  • Python Floats
  • Python Complex Numbers
  • Python List Type
  • Python Tuple Type
  • Python String Type
  • Python Range Type
Collections.abc Module
  • Python Containers
  • Python Hashable
  • Python Iterable
  • Python Iterators
  • Python Sequence
  • Python Mutable Sequence

Create an Account

FREE

Join our community to access more courses.

Create Account

On this page

What does map() do?Basic SyntaxReturning an IteratorConverting map object to list/tupleExamples with lambda functionsMapping over multiple iterablesSummary
      • Blog

      Python Map Function

      What does map() do?

      The map() function applies a given function to each item of one or more iterables (like lists or tuples) and returns an iterator of the results. It's a concise way to transform data without writing explicit loops.

      Basic Syntax

      map(function, iterable, ...)
      
      • function: A function to apply to each item. This can be a built-in function, a user-defined function, or a lambda.
      • iterable: One or more iterables (e.g., lists, tuples).

      map() returns an iterator. To see the results, convert it to a list or tuple, or iterate over it.

      Returning an Iterator

      numbers = [1, 2, 3]
      squared = map(lambda x: x ** 2, numbers)
      print(squared)  # Output: <map object at ...>
      print(list(squared))  # Output: [1, 4, 9]
      

      Converting map object to list/tuple

      You often convert the result to a list or tuple:

      result = map(str, [1, 2, 3])
      print(list(result))  # Output: ['1', '2', '3']
      

      Examples with lambda functions

      words = ["apple", "banana", "cherry"]
      lengths = map(lambda w: len(w), words)
      print(list(lengths))  # Output: [5, 6, 6]
      

      Mapping over multiple iterables

      If you provide multiple iterables, the function must take as many arguments as there are iterables. The mapping stops at the shortest iterable.

      a = [1, 2, 3]
      b = [4, 5, 6]
      sums = map(lambda x, y: x + y, a, b)
      print(list(sums))  # Output: [5, 7, 9]
      

      Summary

      • map() applies a function to each item in one or more iterables.
      • Returns an iterator; convert to list/tuple to see results.
      • Useful for transforming data without explicit loops.
      • Works with built-in, user-defined, or lambda functions.
      • Stops at the shortest iterable if multiple are provided.

      Continue Learning

      Python Variables

      Popular

      Getting Started: Understanding Variables in Python In programming, we often need to store informatio

      Python For Loop

      For You

      Iterating Over Sequences: Understanding the For Loop In Python, the for loop is used to iterate over

      Python Sets

      For You

      Working with Unique Items: Understanding Sets in Python Imagine you have a list of items, and some i

      Personalized Recommendations

      Log in to get more relevant recommendations based on your reading history.