Data processing List of dict without using libraries or import
Question:
I’m studying data processing in python with apply Count, Sum, Group, Sort on a list of dict
list_data = [
{"name": "milk", "quantity": 1, "price_of_one": 2000},
{"name": "egg", "quantity": 12, "price_of_one": 500},
{"name": "egg", "quantity": 6, "price_of_one": 500},
{"name": "milk", "quantity": 2, "price_of_one": 2000},
{"name": "meat", "quantity": 1, "price_of_one": 10000}
]
I want to handle this list with the expected result:
list_data = [
{"name": "egg", "quantity": 18, "total": 9000, "number_occurrences": 2},
{"name": "milk", "quantity": 3, "total": 6000, "number_occurrences": 2},
{"name": "meat", "quantity": 1, "total": 10000, "number_occurrences": 1}
]
As seen, I need SUM by "quantity", SUM by "quantity*price_of_one", GROUP by "name", SORT by SUM of quantity and COUNT number occurrences.
I don’t know How do I make it right?. Is there any way I can get the expected result?
Answers:
You can use dict.setdefault
/dict.get
:
out = {}
for d in list_data:
d2 = out.setdefault(d['name'], {'name': d['name']})
d2['quantity'] = d2.get('quantity', 0) + d['quantity']
d2['total'] = d2.get('total', 0) + d['quantity']*d['price_of_one']
d2['number_occurrences'] = d2.get('number_occurrences', 0)+1
out = list(out.values())
output:
[{'name': 'milk', 'quantity': 3, 'total': 6000, 'number_occurrences': 2},
{'name': 'egg', 'quantity': 18, 'total': 9000, 'number_occurrences': 2},
{'name': 'meat', 'quantity': 1, 'total': 10000, 'number_occurrences': 1},
]
sorted output:
out = sorted(out, key=lambda x: (x['quantity'], x['number_occurrences']),
reverse=True)
output:
[{'name': 'egg', 'quantity': 18, 'total': 9000, 'number_occurrences': 2},
{'name': 'milk', 'quantity': 3, 'total': 6000, 'number_occurrences': 2},
{'name': 'meat', 'quantity': 1, 'total': 10000, 'number_occurrences': 1}]
I’m studying data processing in python with apply Count, Sum, Group, Sort on a list of dict
list_data = [
{"name": "milk", "quantity": 1, "price_of_one": 2000},
{"name": "egg", "quantity": 12, "price_of_one": 500},
{"name": "egg", "quantity": 6, "price_of_one": 500},
{"name": "milk", "quantity": 2, "price_of_one": 2000},
{"name": "meat", "quantity": 1, "price_of_one": 10000}
]
I want to handle this list with the expected result:
list_data = [
{"name": "egg", "quantity": 18, "total": 9000, "number_occurrences": 2},
{"name": "milk", "quantity": 3, "total": 6000, "number_occurrences": 2},
{"name": "meat", "quantity": 1, "total": 10000, "number_occurrences": 1}
]
As seen, I need SUM by "quantity", SUM by "quantity*price_of_one", GROUP by "name", SORT by SUM of quantity and COUNT number occurrences.
I don’t know How do I make it right?. Is there any way I can get the expected result?
You can use dict.setdefault
/dict.get
:
out = {}
for d in list_data:
d2 = out.setdefault(d['name'], {'name': d['name']})
d2['quantity'] = d2.get('quantity', 0) + d['quantity']
d2['total'] = d2.get('total', 0) + d['quantity']*d['price_of_one']
d2['number_occurrences'] = d2.get('number_occurrences', 0)+1
out = list(out.values())
output:
[{'name': 'milk', 'quantity': 3, 'total': 6000, 'number_occurrences': 2},
{'name': 'egg', 'quantity': 18, 'total': 9000, 'number_occurrences': 2},
{'name': 'meat', 'quantity': 1, 'total': 10000, 'number_occurrences': 1},
]
sorted output:
out = sorted(out, key=lambda x: (x['quantity'], x['number_occurrences']),
reverse=True)
output:
[{'name': 'egg', 'quantity': 18, 'total': 9000, 'number_occurrences': 2},
{'name': 'milk', 'quantity': 3, 'total': 6000, 'number_occurrences': 2},
{'name': 'meat', 'quantity': 1, 'total': 10000, 'number_occurrences': 1}]