Python is a powerful and versatile programming language that provides a variety of built-in functions for data manipulation. Among these functions is the map function, which is employed to execute a specified function on every element of an iterable (such as a list or a tuple) and produces a new iterable containing the results.
The map function requires two parameters: a function and an iterable. The function parameter refers to the specific function that will be executed on each element of the iterable, while the iterable parameter signifies the collection of items that the function will be utilized on. Below is the syntax for the map function:
Python map Function Syntax
It has the following syntax:
map(function, iterables)
Parameters
- function: It is a function in which a map passes each item of the iterable.
- iterables: It is a sequence, collection or an iterator object which is to be mapped.
Return
It produces a collection of outcomes by implementing a specified function on every element of an iterable (such as a list, tuple, etc.).
In this article, we will explore the functionality of the map function in Python and provide several examples of its potential applications.
Python map Function Examples
Example 1: Utilizing map to square a list of numbers
A common use case for the map function is to execute a mathematical operation on each item within a list. Below is an illustration of how to use the map function to compute the square of a collection of numbers:
# Python example program for map() function
numbers = [1, 2, 3, 4, 5]
# lambda function defines the squaring operation
squared_numbers = list( map( lambda x : x**2, numbers ))
# print the list of squared numbers
print(squared_numbers)
Output:
[1, 4, 9, 16, 25]
Explanation:
In this illustration, we possess a collection of numerical values that require each of them to be squared. We employ a lambda function to define the operation for this computation, and subsequently, we supply both that function and the collection of numbers to the map function. The map function executes the lambda function on every item within the collection and produces a new iterable containing the squared values.
Example 2: Utilizing map to convert temperatures from Celsius to Fahrenheit
Another application of the map function is to implement a transformation procedure on each item within an iterable. Below is an example demonstrating the use of the map function to convert temperature values from Celsius to Fahrenheit:
# Python example program for map() function
temperatures = [0, 10, 20, 30, 40]
# lambda function defines the conversion formula
fahrenheit_temperatures = list(map( lambda x : (9/5)*x + 32, temperatures ))
# print the list of Fahrenheit temperatures
print(fahrenheit_temperatures)
Output:
[32.0, 50.0, 68.0, 86.0, 104.0]
Explanation:
In this scenario, we possess a collection of temperatures measured in Celsius that require conversion to Fahrenheit. We employ a lambda function to define the conversion formula, and subsequently, we provide both this function and the list of temperatures to the map function. The map function then applies the lambda function to each item in the list, yielding a new iterable containing the converted temperature values.
Example 3: Utilizing map to concatenate strings
The map function can also be employed to execute a string operation on each element within an iterable. Below is an illustration of using the map function to join strings:
# Python example program for map() function
words = ["hello", "world", "python", "map"]
# lambda function defines the string operation
concatenated_words = list(map(lambda x : x.capitalize( ) + "!", words))
# print the list of concatenated words
print(concatenated_words)
Output:
['Hello!', 'World!', 'Python!', 'Map!']
Explanation:
In this illustration, we possess a collection of words, and our goal is to underline each word while incorporating an interjection imprint whenever feasible. We employ a lambda function to define the string operation, after which we supply that function along with the word list to the map function. The map function processes the lambda function for each item in the list and yields a new iterable consisting of the combined strings.
Conclusion
In summary, the map function is a powerful tool within Python that allows users to apply a specific function to every element in an iterable, subsequently producing a new iterable containing the results. This versatile function can be employed for numerical computations, transformation processes, string manipulations, and much more.