Python Intermediate and Advanced

Python Intermediate_002: Map Function (English Version)

codeaddict 2024. 12. 22. 19:04

 

"이 블로그 포스트는 영어로 작성되었지만, 영어에 익숙하지 않은 분들을 위해 다음 글에서 한국어 버전도 제공하고 있습니다."

 

What is the map() function?

The map() function in Python is used to apply a given function to all items in an iterable (like a list, tuple, etc.). This function returns a map object (which is an iterator), which you can convert into a list, tuple, or any other iterable.

The power of map() is in its ability to simplify applying the same operation to multiple items in an iterable without needing to write explicit loops.

Syntax of the map() Function

map(function, iterable, ...)
  • function: A function that you want to apply to each item of the iterable(s).
  • iterable: One or more iterables (lists, tuples, etc.) whose items will be processed by the function.
  • If multiple iterables are passed, the function must take as many arguments as there are iterables.

Example 1: Simple Example

# Define a simple function that doubles a number
def double(x):
    return x * 2

# List of numbers
numbers = [1, 2, 3, 4, 5]

# Use map to apply 'double' to each item in the list
doubled_numbers = map(double, numbers)

# Convert the result to a list and print it
print(list(doubled_numbers))

# Output: [2, 4, 6, 8, 10]

Explanation:

  • We define a function double(x) that multiplies the input by 2.
  • The map() function applies double() to each item in the numbers list.
  • The result is a map object, which we convert to a list for easier viewing. The output will be [2, 4, 6, 8, 10].

Example 2: Intermediate Example (Using Lambda)

Now, let’s use a lambda function to achieve the same result. This will make the code shorter and more concise.

# List of numbers
numbers = [1, 2, 3, 4, 5]

# Use map with a lambda function to double the numbers
doubled_numbers = map(lambda x: x * 2, numbers)

# Convert the result to a list and print it
print(list(doubled_numbers))
# # Output: [2, 4, 6, 8, 10]

Explanation:

  • Instead of defining a separate function, we use a lambda function lambda x: x * 2 directly in the map().
  • This reduces the code and makes it more compact while still achieving the same result.

Example 3: Advanced Example (Multiple Iterables)

In this example, we’ll work with two lists and apply a function that adds corresponding elements from each list.

 

# Function to add corresponding elements from two lists
def add(x, y):
    return x + y

# Two lists of numbers
numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]

# Use map to add corresponding elements of numbers1 and numbers2
sum_numbers = map(add, numbers1, numbers2)

# Convert the result to a list and print it
print(list(sum_numbers))

# Output: [5, 7, 9]

Explanation:

  • The function add(x, y) adds two numbers together.
  • We pass two lists, numbers1 and numbers2, to the map() function.
  • The map() function applies the add() function to the elements of both lists, adding corresponding elements together. The result is [5, 7, 9].