How to transform data with map and complex dictionaries?
You've heard of map
in Python, right? This function is an efficient tool that allows you to transform lists in an elegant way, even when you are working with more complex data structures such as dictionaries. Often, when handling dictionaries, the need arises to transform these elements into other types of data, such as lists of numbers. Exploring how to do this with map
can open up a world of new possibilities.
What is map and how does it work in Python?
The map
function is a fundamental part of Python when we talk about higher-order functions. With it, you can apply a specific function to each item in a list or iterable. The result is a map object, which is an iterable that you can easily convert into a list.
- To use
map
, you need a list of items and a function (lambda or defined) that you want to apply to each item.
- You can transform it for different purposes, such as converting data types.
As a small example, imagine you have a list of dictionaries representing products in a purchase order, and you need to extract only the prices and get a new list with them.
Example dictionary list:
items = [ {"product": "shirt", "price": 100}, {"product": "pants", "price": 300}, {"product": "pants 2", "price": 200},]
To extract the prices:
prices = list(map(lambda item: item["price"], items))print(prices)
How do I add a new attribute using map?
Suppose you need to add a tax attribute to each product. Here, the use of map
is combined with a custom function to calculate the taxes and add them to the original dictionary.
First, define the function to calculate and add the taxes:
def add_taxes(item): item["taxes"] = item["price"] * 0.19 return item.
Then, apply this function to each dictionary using map
:
new_items = list(map(add_taxes, items)).
How do we avoid modifying the original state?
It is important to remember that map
does not modify the state of the original iterable, but creates a new one. However, when working with dictionaries you can change the original state if you are not careful. This is a behavior that can become a headache if it is not what you expected.
- Be sure not to modify the original dictionary if you need to keep it intact.
- Use techniques such as deep copying to ensure that changes are applied only to the new iterable.
Practical example: tax and dictionary management
Through the above example, we have worked with a simple tax calculation and dictionary list to price list transformations:
-
Dictionary list creation:
items = [ {"product": "shirt", "price": 100}, {"product": "pants", "price": 300}, {"product": "pants 2", "price": 200},]
-
Price extraction using Lambda:
prices = list(map(lambda item: item["price"], items)).
-
Adds a new attribute with defined function:
def add_taxes(item): item["taxes"] = item["price"] * 0.19 return item
new_items = list(map(add_taxes, items))
It is essential to remember that accurate handling of transformations and care with the original state of the data are fundamental when practicing programming with Python. Experiment and discover for yourself the various applications that map
can offer in your projects - go ahead, the learning never stops!
Want to see more contributions, questions and answers from the community?