Master Python’s map() Function: Real‑World Examples and Use Cases
This article explains Python’s built‑in map() function, outlines common scenarios such as type conversion, numeric calculations, and preprocessing, and provides ten concrete code examples that demonstrate how to apply map() for data transformation, mathematical operations, and custom logic.
What is map()?
map() is a built‑in higher‑order function in Python that applies a given function to each item of an iterable (such as a list or tuple) and returns an iterator in Python 3 or a list in Python 2, enabling concise batch processing of sequences.
Typical Use Cases
Data type conversion – e.g., turning a list of strings into integers.
Numeric calculations – applying the same arithmetic operation to every element.
String formatting – uniformly formatting each string in a list.
Pre‑filter preprocessing – preparing data before using filter() or list comprehensions.
Parallel processing – supplying per‑task preprocessing logic.
Complex logic – applying custom functions for sophisticated calculations.
Example Code
1. Data type conversion
numbers_str = ['1', '2', '3']
numbers_int = list(map(int, numbers_str))
print(numbers_int) # Output: [1, 2, 3]2. Mathematical operation
nums = [1, 2, 3, 4]
squared = list(map(lambda x: x**2, nums))
print(squared) # Output: [1, 4, 9, 16]3. String formatting
names = ['Alice', 'Bob', 'Charlie']
formatted = list(map(lambda s: f"Hello, {s}!", names))
print(formatted) # Output: ['Hello, Alice!', 'Hello, Bob!', 'Hello, Charlie!']4. Strip whitespace from list elements
text_list = [" hello ", " world ", " python "]
stripped = list(map(str.strip, text_list))
print(stripped) # Output: ['hello', 'world', 'python']5. Process two lists simultaneously
nums1 = [1, 2, 3]
nums2 = [4, 5, 6]
sums = list(map(lambda x, y: x + y, nums1, nums2))
print(sums) # Output: [5, 7, 9]6. Convert strings to uppercase
words = ['apple', 'banana', 'cherry']
upper_words = list(map(str.upper, words))
print(upper_words) # Output: ['APPLE', 'BANANA', 'CHERRY']7. Compute absolute values
negatives = [-1, -2, -3]
absolute_values = list(map(abs, negatives))
print(absolute_values) # Output: [1, 2, 3]8. Prime number test
from math import isqrt
def is_prime(n):
if n < 2:
return False
for i in range(2, isqrt(n) + 1):
if n % i == 0:
return False
return True
numbers = [2, 3, 4, 5, 6]
prime_checks = list(map(is_prime, numbers))
print(prime_checks) # Output: [True, True, False, True, False]9. Generate the first n Fibonacci numbers
def fib(n):
a, b = 0, 1
while n > 0:
yield a
a, b = b, a + b
n -= 1
fibonacci_numbers = list(map(next, [fib(10)] * 10)) # first 10 terms
print(fibonacci_numbers) # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]10. Apply a function to each tuple element
tuples = [(1, 'a'), (2, 'b'), (3, 'c')]
doubled_and_uppercased = list(map(lambda x, y: (x*2, y.upper()), tuples))
print(doubled_and_uppercased) # Output: [(2, 'A'), (4, 'B'), (6, 'C')]Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
