Python map() Function with Examples

What is the Python map() Function?

Python map() function is one of the built-in functions that is available in Python. It is used to apply a function to every item in an iterable (list, tuple, set, etc.) and returns a new iterable containing the results. The map() function takes two arguments:

  1. A function that accepts one argument
  2. An iterable

Syntax of Python map() Function

The syntax of the map() function is as follows:

map(function, iterable)

Here, the function is the function that will be applied to every item in the iterable, and iterable is the iterable that contains the items to be processed.

Examples of Python map() Function

Let’s take a look at some examples to understand how the map() function works.

Example 1: Use of map() with a function

In this example, we will create a function that adds 10 to a given number and then use the map() function to apply it to a list of numbers.

def add_ten(n):
return n + 10
numbers = [1, 2, 3, 4, 5]

result = map(add_ten, numbers)

print(list(result)) # Output: [11, 12, 13, 14, 15]

In this example, we defined a function add_ten() that takes a number n as input and returns the sum of the number and 10.

We then defined a list of numbers numbers and passed it to the map() function along with the add_ten() function. The map() function applied the add_ten() function to each item in the list and returned a new iterable containing the results.

Finally, we used the list() function to convert the results into a list and printed it.

Example 2: Use of map() with a lambda function

In this example, we will use a lambda function instead of a named function to add 10 to a given number.

numbers = [1, 2, 3, 4, 5]
result = map(lambda x: x + 10, numbers)

print(list(result)) # Output: [11, 12, 13, 14, 15]

In this example, we defined a lambda function that takes a number x as input and returns the sum of the number and 10.

We then passed the lambda function to the map() function along with the list of numbers numbers. The map() function applied the lambda function to each item in the list and returned a new iterable containing the results.

Finally, we used the list() function to convert the results into a list and printed it.

Example 3: Use of map() with multiple iterables

In this example, we will use the map() function with multiple iterables.

numbers1 = [1, 2, 3, 4, 5]
numbers2 = [5, 4, 3, 2, 1]
result = map(lambda x, y: x + y, numbers1, numbers2)

print(list(result)) # Output: [6, 6, 6, 6, 6]

In this example, we defined two lists of numbers numbers1 and numbers2.

We then passed these lists to the map() function along with a lambda function that takes two arguments x and y and returns their sum. The map() function applied the lambda function to each pair of items in the lists and returned a new iterable containing the results.

Finally, we used the list() function to convert the results into a list and printed it.

FAQs

1. Can the map() function be used with a function that takes multiple arguments?

Yes, the map() function can be used with a function that takes multiple arguments. In this case, you need to pass multiple iterables to the map() function, where each iterable contains one of the arguments.

2. Can the map() function be used with a dictionary?

Yes, the map() function can be used with a dictionary, but it will only apply the function to the keys of the dictionary. If you want to apply the function to the values of the dictionary, you need to convert them to a list or another iterable.

3. Is the map() function faster than a for loop?

In general, the map() function is faster than a for loop because it uses optimized C code that is executed at the native speed of the interpreter. However, the performance of both methods depends on the size of the iterable and the complexity of the function.

4. Can the map() function be used with a non-iterable object?

No, the map() function can only be used with iterable objects such as lists, tuples, sets, etc. If you want to apply a function to a non-iterable object, you need to convert it into an iterable first.

5. Can the map() function modify the original iterable?

No, the map() function does not modify the original iterable. Instead, it creates a new iterable containing the results of applying the function to each item in the original iterable. If you want to modify the original iterable, you need to do it using a for loop or a list comprehension.

Conclusion

In this article, we discussed what the Python map() function is and how it works. We also looked at some examples of using the map() function with functions and lambda functions, as well as with multiple iterables. Finally, we answered some frequently asked questions about the map() function. By understanding how the map() function works, you can simplify your code and improve its performance.

Facebook
Twitter
LinkedIn
Pinterest

Table of Contents

Related posts