SoFunction
Updated on 2025-03-02

Python Map function nanny level usage tutorial

You should have heard that applying Python allows you to handle the repetitive workload of a day, shortening to a few minutes or even shorter. From then on, work hours are liberated and more efficient working methods are studied. Further improve work efficiency and make work more outstanding. This is not an advertisement, it is a solid news.

This article explores the map function in Python with you, so that you can understand the principle of this function in the shortest time. You can also use fragmented time to consolidate this function, so that you can be more efficient in processing work.

1. Definition of map function

The map function is a commonly used built-in function in Python. It maps the specified sequence according to the provided functions. It can be used to replace the for loop statement, iterate over all specified elements without using any loops, making the code look more concise. The basic call syntax is as follows:

map(func, *iterables)

func: function.

*iterables: One or more sequences.

2. Map function example

Square each element in a sequence

If we want to square each number in a sequence using a loop function, we can use the following code:

new_list = []
for i in [3, 4, 5, 6]:
    new_list.append(i**2)
print(new_list)

Get the result:

[9, 16, 25, 36]

And it is done directly with a line of code of the map function, as follows:

list(map(lambda  x:x**2, [3, 4, 5, 6]))

Get the result:

[9, 16, 25, 36]

where lambda x:x**2 is a function, [3, 4, 5, 6] is the original sequence, and the returned result is a mapping of the original sequence based on the function. However, the result of the map must be displayed through the list function.

Find the sum of the corresponding elements in two sequences

Some people may say that a is a sequence and b is another sequence. It is not easy to find the sum of two sequences, so just a+b is not enough. Let's do a small experiment and see what you will get by using + in python. The code is as follows:

a = [2, 6, 3]
b = [3, 4, 5]
a + b

Get the result:

[2, 6, 3, 3, 4, 5]

You can find that two sequences in python find +, and directly splice the two sequences and return them. Are you smart? Do you think of how to operate using map function? The specific code is as follows:

a = [2, 6, 3]
b = [3, 4, 5]
list(map(lambda a,b:a+b, a, b))

Get the result:

[5, 10, 8]

Add it manually and you will find that the answer is correct.

Find the length of each element in the sequence

Friends who have knowledge of python should know that len(str) means to find the length of str. How should I write the length of each element in the sequence if I want to ask for? The specific code is as follows:

list(map(len, ['white', 'blue', 'green', 'yellow']))

Get the result:

[5, 4, 5, 6]

Manual verification reveals that the answer is correct.

Convert each English element in the sequence to the corresponding capitalization

Friends who have knowledge of python should know that () means converting the letters in str into the corresponding capital. What should I do if I want to convert the English in each element in the sequence to the corresponding capitalization? The specific code is as follows:

list(map(lambda x:(), ['white', 'blue', 'green', 'yellow']))

Get the result:

['WHITE', 'BLUE', 'GREEN', 'YELLOW']

Manual verification reveals that the answer is correct.

Convert strings to numeric sequences

The specific code for converting a string into a numeric sequence is as follows:

list(map(int, '789'))

Get the result:

[7, 8, 9]

Manual verification reveals that the answer is correct.

Extract keys in dictionary

If you want to extract keys in a dictionary, how many methods can you think of? This article provides two methods for your reference.

Method 1:

Use the functions provided by the dictionary to extract, the specific code is as follows:

dict_1 = {'Monday': 'Eat durian', 'Tuesday': 'Eat grapes' , 'Wednesday': 'Eat watermelon', 'Thursday': 'Eat cherries', 'Friday': 'Eat Pramite', 'Saturday': 'Eat grapes', 'Sunday': 'Eat kiwi'}
list(dict_1.keys())

Get the result:

['Monday', 'Tuesday', 'Tuesday', 'Friday', 'Saturday', 'Sunday']

Manual verification reveals that the answer is correct.

Method 2:

Use map function to extract, the specific code is as follows:

list(map(str, {'Monday': 'Eat durian', 'Tuesday': 'Eat grapes' , 'Wednesday': 'Eat watermelon', 'Thursday': 'Eat cherries', 'Friday': 'Eat Pramite', 'Saturday': 'Eat grapes', 'Sunday': 'Eat kiwi'}))

Get the result:

['Monday', 'Tuesday', 'Tuesday', 'Friday', 'Saturday', 'Sunday']

It can be found that the results obtained by Method 2 and Method 1 are consistent. From the above cases, we can find that applying map functions can make the code more concise.

This is the article about the nanny-level usage tutorial for Python Map functions. For more related content of Python Map functions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!