Built in function Grammar/Environments

[Python] How to Use map() Function

This article shows how to use map() function.

Contents

  • What is map function?
  • Basic usage of map function
  • Combination of map function and User-Defined function
  • Combination of map function and lambda function
  • [Appendix] Combination of map function and sum function
  • [Appendix] Combination of map function and split

sponsored link

What is map function?

In the beginning, I explain briefly map() function.

Map() function is built-in function in Python.

So, We can use map() function without "import".

By using map(), function can be applied to all elements of the iterable object( list, tuple, etc.).

This function can be a built-in function, a user-defined function, or a lambda function.

sponsored link

Basic usage of map function

I show basic usage of map() function.

Specify function as the first argument and iterable object (list, tuple, etc.) as the second argument.

map( Function, iterable object )

Map() function returns map object.

The elements cannot be checked as map objects.

The map object is converted to list with list function.

Here is an example code.

The abs function is applied to each element of list.

list_test = [11, 0.3, -5, 3, -1]
print(list_test)
# [11, 0.3, -5, 3, -1]

list_test_m = map(abs, list_test)
print(list_test_m)
# <map object at 0x00000289948332E8>
print(list(list_test_m))
# [11, 0.3, 5, 3, 1]

For reference, here is an example of "for loop".

list_test = [11, 0.3, -5, 3, -1]
print(list_test)
# [11, 0.3, -5, 3, -1]

list_test_abs = []
for num in list_test :
    list_test_abs.append(abs(num))

print(list(list_test_abs))
# [11, 0.3, 5, 3, 1]

sponsored link

Combination of map function and User-Defined function

Map() function can be specified user-defined function as its argument.

Here is an example code.

def square(num) :
    return num*num

list_test = [11, 0.3, -5, 3, -1]

list_test_m = map(square, list_test)
print(list(list_test_m))
# [121, 0.09, 25, 9, 1]

The function "square" is defined to square a number using def statement.

sponsored link

Combination of map function and lambda function

Map() function can be specified lambda function as its argument.

Lambda is useful when you want to use simple calculations.

Here is an example code.

list_test = [11, 0.3, -5, 3, -1]

func = lambda num : num*num

list_test_m = map(func, list_test)
print(list(list_test_m))
# [121, 0.09, 25, 9, 1]

You can also use lambda in the arguments of map() function.

list_test = [11, 0.3, -5, 3, -1]

list_test_m = map(lambda num : num*num, list_test)
print(list(list_test_m))
# [121, 0.09, 25, 9, 1]

sponsored link

[Appendix] Combination of map function and sum function

Here are some other use cases of the map function for reference.

I show an example of combination of map function and sum function.

list_test = [11, 0.3, -5, 3, -1]

result = sum(map(lambda num : num*num, list_test))
print(result)
# 156.09

The squared elements are added with sum() function.

It is not interesting because it is just calculating the sum .

You can also detect the number of elements that contain certain character.

fruits = ["Apple", "Banana", "Orange", "Mango", "Lemon"]

search_char = "a"

result = sum(map(lambda fruit : search_char in fruit, fruits))
print(result)
# 3

sponsored link

[Appendix] Combination of map function and split

I show an example of combination of map function and split.

"Split" is a method to split a string.

By combining the split with the map function, numbers entered as a string can be formatted into list.

num_str = "5 10 -2 1"

num_list =list(map(int, num_str.split()))
print(num_list)
# [5, 10, -2, 1]

The numbers entered as space-delimited string can be formatted into list.

In addition, by using the input function, numbers entered from the keyboard are stored in list.

input_list =list(map(int, input("Please enter a space-separated integer").split()))

print(input_list)
# Please enter a space-separated integer
>> 11 -2 3
# [11, -2, 3]

sponsored link

-Built in function, Grammar/Environments
-,