Mapping Python Lists, Dicts, and Tuples

Mapping in Python means applying an operation for each element of an iterable, such as a list.

For example, let’s square a list of numbers using the map() function:

numbers = [1, 2, 3, 4, 5]

squared_nums = map(lambda x: x ** 2, numbers)
print(list(squared_nums))

Output:

[1, 4, 9, 16, 25]

Mapping in Python

Mapping means transforming a group of values into another group of values.

In Python, you can use the built-in map() function for mapping. The map() function returns a map object. This map object is the result of applying an operation on an iterable, such as a list. You can easily convert this map object back to a list for example by calling the list() function on it.

The syntax of using the map() function:

map(operation, iterable)

Where the operation is a function, or a lambda function, whichever you prefer. The iterable is the group of items for which you apply the operation.

Mapping Python Lists

The mapping works for any iterable in Python. In other words, you can use it on a list.

For example, let’s square a list of numbers. This approach uses a lambda function as the mapping function. If you are unfamiliar with lambdas, feel free to check this guide, or see the next example without lambdas.

numbers = [1, 2, 3, 4, 5]

squared_nums = map(lambda x: x ** 2, numbers)
print(list(squared_nums))

Output:

[1, 4, 9, 16, 25]

Here is the same example. This time we are not using a lambda function, but a regular function instead:

numbers = [1, 2, 3, 4, 5]

def square(number):
    return number ** 2

squared_nums = map(square, numbers)
print(list(squared_nums))

Output:

[1, 4, 9, 16, 25]

This is a result of applying the function square() for each element of the list of numbers. Notice how you don’t need to give the square a parameter in the map function. This is possible because the map function knows what you’re trying to do. It automatically passes each element as an argument to the function one by one.

Mapping Python Dictionaries

You can also map dictionaries in Python using the built-in map() function.

For example, let’s map data such that the values of the key-value pairs become capitalized strings:

data = {
    "name": "jack",
    "address": "imaginary street",
    "education": "mathematican"
}

def capitalize(word):
    return word.capitalize()

data_map = map(lambda pair: (pair[0], capitalize(pair[1])), data.iteritems())
data = dict(data_map)

print(dict(data))

Output:

{'name': 'Jack', 'address': 'Imaginary street', 'education': 'Mathematican'}

There are quite a few lines of code, so let’s clarify how it works:

  • There is data, which is a dictionary of key-value pairs. The values are not capitalized and we want to change that.
  • The capitalize() function takes a string and returns a capitalized version of it.
  • The data_map is a map object. It’s created by applying the capitalize() function for each value of each key-value pair in the data.
  • To convert the data_map back to a dictionary, we use the built-in dict() function.

Mapping Python Tuples

You can also map tuples in Python. This works very similarly to mapping a list.

For example, let’s create a tuple by capitalizing the names of another tuple:

names = ("jamie", "jane", "jack")

def capitalize(word):
    return word.capitalize()

capitalized_names = map(capitalize, names)
print(tuple(capitalized_names))

Output:

('Jamie', 'Jane', 'Jack')

Conclusion

In Python, you can use mapping to transform a group of values into another. To do this use the built-in map() function. This function works by applying a function for each element of the group of values.

For example, you can create a list of squared numbers from a list of numbers using map():

numbers = [1, 2, 3, 4, 5]

squared_nums = map(lambda x: x ** 2, numbers)
print(list(squared_nums))

Thanks for reading. I hope you enjoy it.

Happy coding!

Further Reading

50 Python Interview Questions with Answers

50+ Buzzwords of Web Development