GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Python map() function - GeeksforGeeks Python map() functionLast Updated : 26 Jun, 2026 map() function in Python applies a function to every element of one or more iterables and returns a map object (iterator) containing the transformed results. It is commonly used for element-wise operations and can often replace explicit loops with shorter and more readable code. Let's start with a simple example of using map() to convert a list of strings into a list of integers.
Output [1, 2, 3, 4] Explanation: map() applies int() to each element in 's' which changes their datatype from string to int.
Syntax
Parameters:
Converting map object to a listBy default, map() function returns a map object. In many cases, we may need to convert this iterator to a list to work with the results directly. Example: Let's see how to double each element of the given list.
Output [2, 4, 6, 8] Explanation:
map() with lambdaLambda function can be used with map() to define short transformation logic without creating a separate function.
Output [1, 4, 9, 16] Explanation: lambda x: x ** 2 squares each number and the results are converted into a list. map() with multiple iterablesWe can use map() with multiple iterables if the function we are applying takes more than one argument. Example: In this example, map() takes two iterables (a and b) and applies lambda function to add corresponding elements from both lists.
Output [5, 7, 9] Using map() with Tuplesmap() works with any iterable, including tuples. The following example increments each element of a tuple by 1.
Output (2, 3, 4) Explanation:
String Manipulation with map()Converting strings to UppercaseThis example shows how we can use map() to convert a list of strings to uppercase.
Output ['APPLE', 'BANANA', 'CHERRY'] Explanation: str.upper method is applied to each element in the list fruits using map(). The result is a list of uppercase versions of each fruit name. Extracting first character from stringsIn this example, we use map() to extract the first character from each string in a list.
Output ['a', 'b', 'c'] Explanation: lambda s: s[0] extracts first character from each string in the list words. map() applies this lambda function to every element, resulting map object is converted to a list of the first characters using list(). Removing whitespaces from stringsIn this example, We can use map() to remove leading and trailing whitespaces from each string in a list.
Output ['hello', 'world', 'python'] Explanation: str.strip method removes leading and trailing whitespaces from each string in list strings and map() applies str.strip() to each element. Real World ExampleIn this example, we use map() to convert a list of temperatures from Celsius to Fahrenheit.
Output [32.0, 68.0, 98.6, 212.0] Explanation:
Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |