Python Tutorial - Master Python Programming For Beginners from Scratch

  • Home
  • Getting Started
  • Basics
  • OOP
  • Advanced Python
  • Tkinter
Home » Python Basics » How to Transform List Elements with Python map() Function

How to Transform List Elements with Python map() Function

Summary: in this tutorial, you’ll learn how to use the Python map() function with lists.

Introduction to the Python map() function

When working with a list (or a tuple), you often need to transform the elements of the list and return a new list that contains the transformed element.

Suppose, you want to double every number in the following bonuses list:

bonuses = [100, 200, 300]
Code language: Python (python)

To do it, you can use a for loop to iterate over the elements, double each of them, and add it to a new list like this:

bonuses = [100, 200, 300] new_bonuses = [] for bonus in bonuses: new_bonuses.append(bonus*2) print(new_bonuses)
Code language: Python (python)

Output:

[200, 400, 600]
Code language: Python (python)

Python provides a nicer way to do this kind of task by using the map() built-in function.

The map() function iterates over all elements in a list (or a tuple), applies a function to each and returns a new iterator of the new elements.

The following shows the basic syntax of the map() function:

iterator = map(fn, list)
Code language: Python (python)

In this syntax, fn is the name of the function that will call on each element of the list.

In fact, you can pass any iterable to the map() function, not just a list or tuple.

Back to the previous example, to use the map() function, you define a function that doubles a bonus first and then use the map() function as follows:

def double(bonus): return bonus * 2 bonuses = [100, 200, 300] iterator = map(double, bonuses)
Code language: Python (python)

Or you make this code more concise by using a lambda expression like this:

bonuses = [100, 200, 300] iterator = map(lambda bonus: bonus*2, bonuses)
Code language: Python (python)

Once you have an iterator, you can iterate over the new elements using a for loop.

Or you can convert an iterator to a list by using the the list() function:

bonuses = [100, 200, 300] iterator = map(lambda bonus: bonus*2, bonuses) print(list(iterator))
Code language: Python (python)

More examples of Python map() function with lists

Let’s take some more examples of using the Python map() function with lists.

1) Using the Python map() function for a list of strings

The following example uses the map() function to returns a new list where each element is transformed into the proper case:

names = ['david', 'peter', 'jenifer'] new_names = map(lambda name: name.capitalize(), names) print(list(new_names))
Code language: Python (python)

Output:

['David', 'Peter', 'Jenifer']
Code language: Python (python)

2) Using the Python map() function to a list of tuples

Suppose that you have the following shopping cart represented as a list of tuples:

carts = [['SmartPhone', 400], ['Tablet', 450], ['Laptop', 700]]
Code language: Python (python)

And you need to calculate the tax amount for each product with a 10% tax 10%. In addition, you need to add the tax amount to the third element of each item in the list.

The return list should be something like this:

[['SmartPhone', 400, 40.0], ['Tablet', 450, 45.0], ['Laptop', 700, 70.0]]
Code language: Python (python)

In order to do so, you can use the map() function to create a new element of the list and add the new tax amount to each like this:

carts = [['SmartPhone', 400], ['Tablet', 450], ['Laptop', 700]] TAX = 0.1 carts = map(lambda item: [item[0], item[1], item[1] * TAX], carts) print(list(carts))
Code language: Python (python)

Summary

  • Use the Python map() to call a function on every item of a list and returns an iterator.
  • Was this tutorial helpful ?
  • YesNo
Previous Python Iterables
Next How to Filter List Elements in Python

Getting Started

  • What is Python
  • Install Python
  • Setup VS Code for Python
  • Develop Python Hello World Program

Python Fundamentals

  • Python Syntax
  • Variables
  • Strings
  • Numbers
  • Boolean
  • Constants
  • Comments
  • Type Conversion

Control Flow

  • if…else
  • Ternary Operator
  • for Loop
  • while Loop
  • break
  • continue
  • pass

Functions

  • Python Functions
  • Default Parameters
  • Keyword Arguments
  • Recursive Functions
  • Lambda Expressions
  • Function Docstrings

Python List

  • List
  • Tuple
  • Sorting a List in Place: sort()
  • Sorting a List: sorted()
  • Slicing a List: [::]
  • Unpacking a List
  • Iterating over a List: for loop
  • Finding Index of an Element: index()
  • Iterables
  • Transform List Elements: map()
  • Filtering List Elements: filter()
  • Reducing List Elements: reduce()
  • List Comprehensions

Python Dictionary

  • Dictionary
  • Dictionary Comprehension

Python Set

  • Set
  • Set Comprehension
  • Union of Sets
  • Intersection of Sets
  • Difference between Sets
  • Symmetric Difference of Sets
  • Subset
  • Superset
  • Disjoint Sets

Exception Handling

  • try…except
  • try…except…finally
  • try…except…else

Python Loop with Else Clause

  • for…else
  • while…else
  • do…while Emulation

More on Functions

  • Unpacking Tuples
  • The *args Parameters
  • The **kwargs Parameters
  • Partial Functions

Modules

  • Modules
  • Module Search Path
  • Python __name__
  • Packages

File I/O

  • Reading a Text File
  • Writing to a Text File
  • Creating a Text File
  • Checking If a File Exists
  • Reading from a CSV File
  • Writing to a CSV File

Managing Third-party Packages

  • Python Package Index (PyPI) & PIP
  • Python Virtual Environments
  • Install pipenv on Windows
  • Using pipenv Tool

About pythontutorial.net

Pythontutorial.net helps you master Python programming from scratch fast.

Site Links

  • Home
  • Contact
  • About
  • Privacy Policy

Recent Python Tutorials

  • Tkinter StringVar
  • Tkinter PhotoImage
  • Tkinter OptionMenu
  • Tkinter ScrolledText
  • How to Change the Appearances of Widgets Dynamically Using Ttk Style map() Method

Python References

  • Python String Methods
  • Python MySQL
  • Python PostgreSQL
  • Python Oracle
  • Python SQLite

Copyright © 2021 · by pythontutorial.net. All rights reserved. Log in