Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
eval in Python
Next article icon

The map() function is used to apply a given function to every item of aniterable, such as alist ortuple, and returns amap object (which is an iterator).

Let's start with a simple example of using map() to convert a list of strings into a list of integers.

Python
s=['1','2','3','4']res=map(int,s)print(list(res))

Output
[1, 2, 3, 4]

Explanation:Here, we used the built-inint function to convert each string in the list s into an integer. Themap()function takes care of applying int() to every element

Syntax of the map() function

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

map(function, iterable)

Parameter:

  • function: The function we want to apply to every element of the iterable.
  • iterable: The iterable whose elements we want to process.

Note:We can also pass multiple iterables if our function accepts multiple arguments.

Converting map object to a list

By default, themap()function returns amap object, which is aniterator. In many cases, we will need to convert thisiterator to alist to work with the results directly.

Example: Let's see how to double each elements of the given list.

Python
a=[1,2,3,4]# Using custom function in "function" parameter# This function is simply doubles the provided numberdefdouble(val):returnval*2res=list(map(double,a))print(res)

Output
[2, 4, 6, 8]

Explanation:

  • Themap() function returned an iterator, which we then converted into a list usinglist(). This is a common practice when working withmap()
  • We used a custom function to double each value in the lista. The result was mapped and converted into a list for easy display.

map() with lambda

We can use alambda function instead of a custom function withmap() to make the code shorter and easier. Let's see how to improve the above code for better readability.

Python
a=[1,2,3,4]# Using lambda function in "function" parameter# to double each number in the listres=list(map(lambdax:x*2,a))print(res)

Output
[2, 4, 6, 8]

Explanation: We used lambdax: x * 2 to double each value in the lista. The result was mapped and converted into a list for easy display.

Using map() with multiple iterables

We can usemap() with multiple iterables if the function we are applying takes more than one argument.

Example: In this example,map()takes two iterables (a andb) and applies thelambda function to add corresponding elements from both lists.

Python
a=[1,2,3]b=[4,5,6]res=map(lambdax,y:x+y,a,b)print(list(res))

Output
[5, 7, 9]

Examples of map() function

Converting to uppercase

This example shows how we can usemap()to convert a list of strings to uppercase.

Python
fruits=['apple','banana','cherry']res=map(str.upper,fruits)print(list(res))

Output
['APPLE', 'BANANA', 'CHERRY']

Explanation: Thestr.upper method is applied to each element in the list fruits usingmap(). The result is a list of uppercase versions of each fruit name.

Extracting first character from strings

In this example, we usemap() to extract the first character from each string in a list.

Python
words=['apple','banana','cherry']res=map(lambdas:s[0],words)print(list(res))

Output
['a', 'b', 'c']

Explanation:The lambda function s: s[0]extracts the first character from each string in the list words. map() applies this lambda function to every element, resulting in a list of the first characters of each word.

Removing whitespaces from strings

In this example, We can usemap() to remove leading and trailing whitespaces from each string in a list.

Python
s=['  hello  ','  world ',' python  ']res=map(str.strip,s)print(list(res))

Output
['hello', 'world', 'python']

Explanation: Thestr.strip method removes leading and trailing whitespaces from each string in the list strings. Themap() function appliesstr.strip to each element and returning a list of trimmed strings.

Calculate fahrenheit from celsius

In this example, we usemap() to convert a list of temperatures from Celsius to Fahrenheit.

Python
celsius=[0,20,37,100]fahrenheit=map(lambdac:(c*9/5)+32,celsius)print(list(fahrenheit))

Output
[32.0, 68.0, 98.6, 212.0]

Explanation: The lambda functionc: (c * 9/5) + 32 converts each Celsius temperature to Fahrenheit using the standard formula. The map() function applies this transformation to all items in the list celsius.


Map() Function in Python
Visit Courseexplore course icon

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp