Mastering Python's map() Function: Syntax, Examples, and Best Practices
Python’s built‑in map() applies a function to every item in an iterable (list, tuple, set, string, dictionary, etc.) and returns a new iterable. It’s a cornerstone for functional‑style programming in Python.
What You’ll Learn
- Syntax and core parameters
- How
map()works under the hood - Using
map()with built‑in functions - Applying
map()to strings, lists, tuples, dictionaries, and sets - Leveraging lambda expressions with
map() - Combining multiple iterators in a single call
Syntax
map(function, iterable1, iterable2, ... iterableN)
Parameters
- function: A mandatory callable that will be applied to each element.
- iterable(s): One or more iterables. The function receives one element from each iterable per call.
Return Value
The result is a map object—a lazy iterable that yields transformed items. Convert it to a list, tuple, or set to consume the values.
How map() Works
Below is a simple example that squares each number in a list.
def square(n):
return n * n
my_list = [2, 3, 4, 5, 6, 7, 8, 9]
updated = map(square, my_list)
print(list(updated)) # [4, 9, 16, 25, 36, 49, 64, 81]
The map object is lazy; converting it to a list forces evaluation.
Using map() with Built‑In Functions
Combine map() with any built‑in that accepts a single argument. For example, rounding floating numbers:
values = [2.6743, 3.63526, 4.2325, 5.9687967, 6.3265, 7.6988, 8.232, 9.6907] rounded_vals = map(round, values) print(list(rounded_vals)) # [3, 4, 4, 6, 6, 8, 8, 10]
Map Over Strings
Strings are iterable, so map() can transform each character or word.
def to_upper(s):
return s.upper()
text = "welcome to guru99 tutorials!"
print("".join(map(to_upper, text))) # WELCOME TO GURU99 TUTORIALS!
Map Over Lists of Numbers
def times_ten(n):
return n * 10
nums = [2, 3, 4, 5, 6, 7, 8, 9]
print(list(map(times_ten, nums))) # [20, 30, 40, 50, 60, 70, 80, 90]
Map Over Tuples
def to_upper_str(s):
return s.upper()
languages = ("php", "java", "python", "c++", "c")
print(list(map(to_upper_str, languages))) # ['PHP', 'JAVA', 'PYTHON', 'C++', 'C']
Map Over Dictionaries and Sets
When iterating over a dictionary, map() receives its keys. For a set, the order is arbitrary.
def times_ten(n):
return n * 10
sample_set = {2, 3, 4, 5, 6, 7, 8, 9}
print(sorted(map(times_ten, sample_set))) # [20, 30, 40, 50, 60, 70, 80, 90]
Using Lambda Functions
Anonymous functions keep code concise:
nums = [2, 3, 4, 5, 6, 7, 8, 9] print(list(map(lambda x: x * 10, nums))) # [20, 30, 40, 50, 60, 70, 80, 90]
Multiple Iterators
Example 1: Adding Two Lists
def add(a, b):
return a + b
list1 = [2, 3, 4, 5, 6, 7, 8, 9]
list2 = [4, 8, 12, 16, 20, 24, 28]
print(list(map(add, list1, list2))) # [6, 11, 16, 21, 26, 31, 36]
Example 2: Concatenating List and Tuple Elements
def concat(a, b):
return f"{a}_{b}"
letters = ['a', 'b', 'b', 'd', 'e']
brands = ('PHP', 'Java', 'Python', 'C++', 'C')
print(list(map(concat, letters, brands))) # ['a_PHP', 'b_Java', 'b_Python', 'd_C++', 'e_C']
Key Takeaways
map()transforms each element of an iterable with a given function.- It returns a lazy iterator; convert to
list,tuple, orsetto materialise results. - Compatible with any callable—including built‑ins, user‑defined functions, and lambdas.
- Supports multiple iterables; the function must accept a matching number of arguments.
- Great for clean, functional‑style code and for processing large datasets efficiently.
Python
- Python Print() Function: A Practical Guide with Examples
- Mastering Python’s strip() Method: Comprehensive Guide & Practical Examples
- Master Python’s str.count(): How to Count Characters & Substrings with Examples
- Master Python `format()` Strings with Clear Examples
- Master Python's String.find() Method: Syntax, Examples & Alternatives
- Master Python Lambda Functions: Practical Examples & Best Practices
- Python round() Function Explained with Practical Examples
- Python timeit() – Measuring Execution Time with Practical Examples
- Python list.count(): Expert Guide with Practical Examples
- Python Module Importing – A Practical Guide with Examples