map() Function in Python: Transform Your List Elements

Conversion of one parameter to another is always a requirement in industry. Be it converting kilometers to miles or kilograms to grams or Celsius to Fahrenheit, it all helps you in day-to-day life. But, it is hard to calculate it manually, and what if you are in a situation where you have to deploy a conversion scale?

It sounds like a tedious task but fear not, Python has an easy solution to it. You learned all about functions in Python in the previous article and one of those specialized functions is the 'map()' function which allows you to transform elements in a list. Let's learn more about this function in upcoming sections.

map() Function in Python:

The 'map()' function in Python offers a convenient and powerful way to transform elements within a list by applying a specified function. By taking in a function and an iterable, such as a list, the 'map()' function efficiently applies the function to each element, producing a new iterable with the transformed elements.

This capability allows for streamlined data manipulation and transformation tasks. Whether it's converting units of measurement, applying mathematical operations, or performing custom transformations, the 'map()' function empowers Python developers to easily and effectively transform list elements.

Syntax for map() Function in Python:

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

 >>> map(function, iterable)

Here's a breakdown of the components:

  • 'function': This is the function that you want to apply to each element in the 'iterable'. It can be a built-in function, a user-defined function, or a lambda function.
  • 'iterable': This refers to the sequence or iterable objects, such as a list, tuple, or string, that you want to transform. The map() function will iterate over each element in the 'iterable' and apply the 'function' to it.

The 'map()' function returns a map object, which is an iterator that yields the transformed elements one by one. To access the transformed elements, you can convert the map object to a list or iterate over it using a loop.

NOTE: It's important to note that the 'map()' function applies the function to each element in a one-to-one correspondence. This means that the function should accept one argument and return the transformed value for that argument.

Example Programs using map() Function in Python:

You have learned about the syntax in the previous section and it is understandable if you feel confused all about it but don't worry, I will explain the learned concept with example programs:

Example 1: Multiply a list of elements with 2

 >>> def double(n):
 >>>     return n * 2
 >>> numbers = [1, 2, 3, 4, 5]
 >>> result = map(double, numbers)

In this example, the 'double()' function is defined to multiply a number by 2. The numbers list contains the elements to be transformed. The 'map()' function is used with double as the function and numbers as the iterable. The result variable will hold the map object that contains the transformed elements.

To access the transformed elements, you can convert the result map object to a list:

 >>> transformed_numbers = list(result)
 >>> print(transformed_numbers)
Output:

[2, 4, 6, 8, 10]

In this case, the map object is converted to a list using the 'list()' function, and the resulting list, 'transformed_numbers', will contain the elements after applying the 'double()' function to each element of the numbers list.

Example 2: Convert the given temperature from Celsius to Fahrenheit.

 >>> def celsius_to_fahrenheit(celsius):
 >>>     return (celsius * 9/5) + 32
 >>> temperatures_celsius = [25, 30, 15, 20, 35]
 >>> temperatures_fahrenheit = list(map(celsius_to_fahrenheit, temperatures_celsius))
 >>> print(temperatures_fahrenheit)
Output:

[77.0, 86.0, 59.0, 68.0, 95.0]

In this example, we define the function 'celsius_to_fahrenheit()' that converts a temperature from Celsius to Fahrenheit using the conversion formula. We have a list of temperatures in Celsius, 'temperatures_celsius', and we want to convert each Celsius temperature to Fahrenheit using the 'celsius_to_fahrenheit()' function.

By using the 'map()' function, we pass the 'celsius_to_fahrenheit' function as the first argument and temperatures_celsius as the second argument. The 'map()' function applies the 'celsius_to_fahrenheit()' function to each element in 'temperatures_celsius', resulting in a new iterable. We convert the iterable to a list using the 'list()' function and store it in 'temperatures_fahrenheit'. Finally, we print the transformed temperatures in Fahrenheit.

Example 3: map() function using lambda function

The 'map()' function is also commonly used with lambda functions, which are anonymous functions that don't require a separate definition. Here's an example using a lambda function to square each element in a list:

 >>> numbers = [1, 2, 3, 4, 5]
 >>> squared_numbers = list(map(lambda x: x**2, numbers))
 >>> print(squared_numbers)
Output:

[1, 4, 9, 16, 25]

In this example, we define a lambda function that squares a number (x) using the expression 'x**2'. We apply this lambda function to each element in the 'numbers' list using the 'map()' function, resulting in a new iterable of squared numbers stored in 'squared_numbers'.

Conclusion:

In conclusion, The 'map()' function provides a concise and efficient way to transform elements in a list by applying a function to each element. It is a versatile tool in Python for various data manipulation and transformation tasks.


Learn via Video Course

Python Logo

Python

6109

7 hrs