Python tutorial- map built-in function

map function

The map function returns a list of the results of applying the function to the items of the argument sequence(s). For example, you have a list: [1, 2, 3, 4, 5], and you want to multiply each element of the list by 2. So you will get a new list: [2, 4, 6, 8, 10]. The easy way to solve this problem is to use map function.
>>> lst1=range(1,6)
>>> def multiplyBy2(a):
            return a*2
>>> lst2=map(multiplBy2,lst1)
>>> lst2=map(multiplyBy2,lst1)
>>> lst2
[2, 4, 6, 8,10]
>>>    Note: The a parameter is important because it refers to each element of the list lst1.


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.