ZetCode

Python map Function

Last modified April 11, 2025

This comprehensive guide explores Python's map function, which applies a function to each item in an iterable. We'll cover basic usage, lambda functions, multiple iterables, and practical examples.

Basic Definitions

The map function applies a given function to each item of an iterable and returns a map object (an iterator). It's a fundamental tool for functional programming in Python.

Key characteristics: lazy evaluation (returns iterator), works with any callable, supports multiple iterables. The map object can be converted to other sequences like lists or tuples.

Basic Numeric Transformation

Here's simple usage with numbers showing how map can transform each element in a list by applying a function.

basic_map.py
def square(x):
    return x ** 2

numbers = [1, 2, 3, 4, 5]
squared = map(square, numbers)

print(list(squared))  # [1, 4, 9, 16, 25]

This example shows map applying the square function to each number in the list. The result is an iterator that we convert to a list for display.

Note that the original numbers list remains unchanged. map creates a new iterator with transformed values.

Using Lambda Functions

map is often used with lambda functions for concise one-time transformations. This example demonstrates this common pattern.

lambda_map.py
numbers = [1, 2, 3, 4, 5]
doubled = map(lambda x: x * 2, numbers)

print(list(doubled))  # [2, 4, 6, 8, 10]

Here we use a lambda function to double each number. The lambda syntax is compact and avoids defining a separate named function for simple operations.

This approach is particularly useful when the transformation logic is simple and won't be reused elsewhere in the code.

Multiple Iterables

map can process multiple iterables in parallel. The function receives one item from each iterable at each step.

multi_map.py
def add(a, b):
    return a + b

nums1 = [1, 2, 3]
nums2 = [4, 5, 6]
result = map(add, nums1, nums2)

print(list(result))  # [5, 7, 9]

This example adds corresponding elements from two lists. The add function receives pairs (1,4), (2,5), and (3,6) in sequence.

The resulting iterator stops when the shortest input iterable is exhausted. This behavior is consistent with other Python functions like zip.

Type Conversion

map is commonly used for type conversion of sequence elements. This example converts strings to integers.

type_map.py
str_numbers = ["1", "2", "3", "4"]
int_numbers = map(int, str_numbers)

print(list(int_numbers))  # [1, 2, 3, 4]

Here we use Python's built-in int function to convert each string to an integer. This is a clean way to transform sequence types.

Similar patterns work with other type constructors like float, str, or bool for different conversion needs.

Processing Text Data

This example shows how map can process text data by applying string methods to each element in a sequence.

text_map.py
names = ["alice", "bob", "charlie"]
capitalized = map(str.capitalize, names)

print(list(capitalized))  # ["Alice", "Bob", "Charlie"]

We use the str.capitalize method to properly capitalize each name. The method is bound to each string element during iteration.

This pattern works with any string method, making map useful for batch text processing operations.

Best Practices

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.