The Python map function applies the user given function to each item in an iterable such as list, tuple, etc. Next, this Python map function returns a list of the result values.
In this section, we discuss how to use this map function in Python Programming with example. The basic syntax of the Python map function is
map(function_name, iterables,...)
Python map Values Example
In this Python map function example, we declared a function called addition. This function adds a number to itself and returns that value. Next, we declared a list of numbers from 10 to 50.
Next, we used Python map function to assign that addition function to each item in a numbers list.
By default, the map function returns the map object as an output value. So, we have to convert that map object to any of the Iterable. The last print statement converts the map object to list and prints the list of mapped values.
# Python map function def addition(num): return num + num numbers = [10, 20, 30, 40, 50] map_result = map(addition, numbers) print(list(map_result))
TIP: Please refer Python List article to understand the lists.
Python String map Example
We are using both the numeric values and the list of characters. Here, map (addition, chars) means, for each character in the char list, it calls the addition function. + symbol on characters or strings concat them.
def addition(num): return num + num chars = ['a', 'b', 'c', 'd', 'e'] numbers = [10, 20, 30, 40, 50] map_result = map(addition, chars) print('Chars List = ', list(map_result)) result = map(addition, numbers) print('Numbers List = ', list(result))
Python map Multiple arguments Example
Until now, we are using this map function on one iterable (single list). In this example, we created a function that accepts two arguments and returns the sum of those values. Next, we declared two lists of numeric values.
Here, map(addition, numbers1, numbers2) means it takes first value from two lists and apply addition function and so on. For example, map(addition, 10, 150) becomes 160.
Next, we are using the same map function on fruits (string list). It performs concatenation.
def addition(a, b): return a + b numbers1 = [10, 20, 30, 40, 50] numbers2 = [150, 250, 350, 450, 550] map_result = map(addition, numbers1, numbers2) print(list(map_result)) fruits1 = ['apple', 'orange', 'kiwi'] fruits2 = ['banana', 'cherry', 'berry'] result = map(addition, fruits1, fruits2) print(list(result))
Python map with built-in functions Example
Here, we are using the built-in function as the mapping functions. For this Python demo, we used factorial function, and len functions.
Here, the factorial function finds the factorial of each list item a numbers list. Next, the len function finds the length of each item or fruit in the list of the fruits.
import math def factorial_func(num): return math.factorial(num) def len_func(x): return len(x) numbers = [1, 2, 3, 4, 5, 6, 7] map_result = map(factorial_func, numbers) print(list(map_result)) fruits = ['apple', 'orange', 'kiwi', 'banana', 'pineapple'] result = map(len_func, fruits) print(list(result))
Python map lambda Example
You can use the lambda function or expression inside this map function to make the code easily readable.
map(lambda x: x * x, numbers) returns the square of each item in numbers list. map(lambda a:a.upper(), fruits) uses upper function and converts each fruit to uppercase. Next, map(lambda a:a.capitalize(), fruits) uses capitalize function to capitalise the first character in each fruit.
numbers = [10, 20, 30, 40, 50, 60] map_result = map(lambda x: x * x, numbers) print(list(map_result)) fruits = ['apple', 'orange', 'kiwi', 'banana', 'pineapple'] result = map(lambda a:a.upper(), fruits) print(list(result)) result2 = map(lambda a:a.capitalize(), fruits) print(list(result2))
Python map lambda Multiple arguments Example
Let me use multiple iterables in map function along with lambda. This example accepts two lists and adds each item in one list with another list.
numbers1 = [10, 20, 30, 40, 50] numbers2 = [150, 250, 350, 450, 550] map_result = map(lambda x, y: x + y, numbers1, numbers2) print(list(map_result)) fruits1 = ['apple', 'orange', 'kiwi'] fruits2 = ['banana', 'cherry', 'berry'] result = map(lambda a, b: a + b, fruits1, fruits2) print(list(result))
Python map lambda Multiple functions Example
Until now, we show how to use a single map function with a list of values. However, you can also apply multiple functions on each item using this map function.
Here, we declared or defined three functions. Next, we created a list of these three function names.
First, we are using for loop with range to iterate values from 1 to 9. Next, we used map function with lambda to assign each for loop item to functions list. It means for 1, the map function call square_func, tripple_func and four_func similarly for 2 and so on.
def square_func(num): return num**2 def tripple_func(num): return num * num * num def four_func(num): return num**4 function_list = [square_func, tripple_func, four_func] for i in range(1, 10): number = map(lambda x: x(i), function_list) print("For ", i, " = " , list(number))
Convert Python map to list, set and tuple
Until now, we are showing the mapped result as a list. However, you can convert them to any iterable such as tuple or set.
- For list, use list(mapped_result).
- For tuple, use tuple(mapped_result).
- Same for the set. Use set(mapped_result).
First, we created a function to find the square of a given number and then declared a numbers list. Next, we are converting the Python map function result to list, tuple, and set.
def square_func(num): return num**2 numbers = [10, 20, 30, 40, 50] list_result = map(square_func, numbers) my_list = list(list_result) print(my_list) tuple_result = map(square_func, numbers) my_tuple = tuple(tuple_result) print(my_tuple) set_result = map(square_func, numbers) my_set = set(set_result) print(my_set)
Python map list and tuple example
You can use this Python map function to convert the list to a nested list or nested tuple. Here, we are calling the list function along with the list of the fruits inside a map function.
fruits = ['apple', 'berry', 'kiwi'] result = map(list, fruits) print(list(result)) result2 = map(tuple, fruits) print(tuple(result2))