Python | map() function

Python | map() function

Introduction: The map() is a type of higher order function which is used to apply a given function to each item of an iterable (such as a list, tuple, or set) and returns an iterator that yields the results. In this article, we’ll explore the concept of map() function in Python, explore its syntax, usage, and provide practical examples to illustrate its functionality.

Syntax of map() Function:

map(function, iterable)

Where:
function: The function to apply to each item of the iterable.
iterable: The iterable (list, tuple, set, etc.) whose elements will be passed to the function.

Example 1: Using map() with a function.

# Define a function to square a number
def square(x):
    return x ** 2

# Apply the square function to each element of the list
numbers = [10, 20, 30, 40, 50]
squared_numbers = map(square, numbers)

# Convert the map object into a list
squared_numbers_list = list(squared_numbers)

print(squared_numbers_list) # Output: [100, 400, 900, 1600, 2500]

In this example, the square() function is applied to each element of the numbers list using the map() function, resulting in a new list containing the squared values.

Example 2: Using map() with lambda function.

# Apply a lambda function to each element of the list
numbers = [10, 20, 30, 40, 50]
squared_numbers = map(lambda x: x ** 2, numbers)

# Convert the map object into a list
squared_numbers_list = list(squared_numbers)

print(squared_numbers_list) # Output: [100, 400, 900, 1600, 2500]

In this example, a lambda function is used to square each element of the numbers list, and the map() function is applied to generate the squared values.

Example 3: Using map() with multiple iterables.

# Define a function to add corresponding elements of two lists
def add(x, y):
    return x + y

# Apply the add function to corresponding elements of two lists
numbers1 = [10, 20, 30]
numbers2 = [40, 50, 60]
result = map(add, numbers1, numbers2)

# Convert the map object into a list
result_list = list(result)

print(result_list) # Output:[50, 70, 90]

In this example, the add() function is applied to corresponding elements of two lists (numbers1 and numbers2) using the map() function, resulting in a new list containing the sum of each pair of elements.

Conclusion: The map() function in Python provides a concise and efficient way to apply a function to each element of an iterable. Whether you’re working with lists, tuples, sets, or other iterable types, map() can streamline your code and make it more readable. By understanding the syntax and examples provided in this article, you’ll be well-equipped to leverage the map() function in your Python programming tasks.

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds