Javatpoint Logo
Javatpoint Logo

Python map() Function

Python is a strong and flexible programming language that offers different built-in functions to perform operations on data. One such function is the map() function, which is utilized to apply a function to each element of an iterable (like a list or a tuple) and returns a new iterable with the outcomes.

The map() function takes two arguments: a function and an iterable. The function contention is the function that will be applied to every element of the iterable, and the iterable contention is the iterable that the function will be applied to. Here is the syntax of the map() function:

Syntax:

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 returns a list of results after applying a given function to each item of an iterable(list, tuple etc.)

In this article, we will investigate the map() function in Python and give a few examples of how it tends to be utilized.

Python map() Function Examples

Example 1: Utilizing map() to square a list of numbers

One normal utilization of the map() function is to apply a numerical activity to every element of a list. Here is an example of utilizing the map() function to square a list of numbers:

Code:

Output:

[1, 4, 9, 16, 25]

In this example, we have a list of numbers and we need to square every one of them. We utilize the lambda function to characterize the figuring out activity, and afterward we pass that function and the list of numbers to the map() function. The map() function applies the lambda function to every element of the list and returns a new iterable with the squared numbers.

Example 2: Utilizing map() to convert temperatures from Celsius to Fahrenheit

One more utilization of the map() function is to apply a change recipe to every element of an iterable. Here is an example of utilizing the map() function to convert temperatures from Celsius to Fahrenheit:

Code:

Output:

[32.0, 50.0, 68.0, 86.0, 104.0]

In this example, we have a list of temperatures in Celsius and we need to convert them to Fahrenheit. We utilize the lambda function to characterize the change recipe, and afterward we pass that function and the list of temperatures to the map() function. The map() function applies the lambda function to every element of the list and returns a new iterable with the converted temperatures.

Example 3: Utilizing map() to concatenate strings

The map() function can likewise be utilized to apply a string activity to every element of an iterable. Here is an example of utilizing the map() function to concatenate strings:

Code:

Output:

['Hello!', 'World!', 'Python!', 'Map!']

In this example, we have a list of words and we need to underwrite each word and add an interjection imprint as far as possible. We utilize the lambda function to characterize the string activity, and afterward we pass that function and the list of words to the map() function. The map() function applies the lambda function to every element of the list and returns a new iterable with the concatenated strings.

Conclusion:

The map() function is an incredible asset in Python that can be utilized to apply a function to each element of an iterable and return a new iterable with the outcomes. A flexible function can be utilized with numerical operations, transformation recipes, string operations, from there, the sky is the limit.


Next TopicPython Functions





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA