Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

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

map(function, iterable1, iterable2, ... iterableN)

Parameters

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

Python

  1. Python Print() Function: A Practical Guide with Examples
  2. Mastering Python’s strip() Method: Comprehensive Guide & Practical Examples
  3. Master Python’s str.count(): How to Count Characters & Substrings with Examples
  4. Master Python `format()` Strings with Clear Examples
  5. Master Python's String.find() Method: Syntax, Examples & Alternatives
  6. Master Python Lambda Functions: Practical Examples & Best Practices
  7. Python round() Function Explained with Practical Examples
  8. Python timeit() – Measuring Execution Time with Practical Examples
  9. Python list.count(): Expert Guide with Practical Examples
  10. Python Module Importing – A Practical Guide with Examples