Iterate over list of dict and group item by key

Vlad

I have a list of dicts:

my_list = [
    {'name': 'AAA', 'date': '2018-05-14', 'price': 20.0},
    {'name': 'AAA', 'date': '2018-05-15', 'price': 22.0},
    {'name': 'AAA', 'date': '2018-05-16', 'price': 30.0},
    {'name': 'BBB', 'date': '2018-05-14', 'price': 15.0},
    {'name': 'BBB', 'date': '2018-05-15', 'price': 32.0}
    ]

My question is how can I iterate over that list to produce a list in this format?

parsed_list = [
    {'name': 'AAA', 'data': [['2018-05-14', 20.0], ['2018-05-15', 22.0], ['2018-05-16', 30.0]]},
    {'name': 'BBB', 'data': [['2018-05-14', 15.0], ['2018-05-15', 32.0]]}
    ]

I tried approach described in this question: Python: group list items in a dict but I need a different output format and I can't figure out what I need to change.

student

One way may be to use defaultdict:

from collections import defaultdict

my_list = [
    {'name': 'AAA', 'date': '2018-05-14', 'price': 20.0},
    {'name': 'AAA', 'date': '2018-05-15', 'price': 22.0},
    {'name': 'AAA', 'date': '2018-05-16', 'price': 30.0},
    {'name': 'BBB', 'date': '2018-05-14', 'price': 15.0},
    {'name': 'BBB', 'date': '2018-05-15', 'price': 32.0}
    ]

tmp = defaultdict(list)

for item in my_list:
    tmp[item['name']].append([item['date'],item['price']])

parsed_list = [{'name':k, 'data':v} for k,v in parsed_list.items()]
print(parsed_list)

Result:

[{'name': 'AAA', 'data': [['2018-05-14', 20.0], 
                          ['2018-05-15', 22.0], ['2018-05-16', 30.0]]}, 
 {'name': 'BBB', 'data': [['2018-05-14', 15.0], ['2018-05-15', 32.0]]}]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Iterate over a list of dict

Iterate over a dict except for x item items

Python: Iterate over dict and list at the same time

Iterate with Ansible with_dict over list of a dictionaries

How to create and iterate over a method group list

How to iterate a key over a list of lists in a dictionary?

How to iterate over a list with key value pair

Group list of dict results using python based on first key from each item

How to iterate over python dict from the chosen key to it again?

List of lists to dict with every item in sublist as key

How can I iterate over a list within a dict structure?

How to iterate over a list of values in a dict using jinja?

Iterate over array of objects for group by Key and nested array of objects

How to iterate over dictionaries in a list and extract key values to a separate list

Python - Iterate over a dict with list as values and create new dict based on list values

How iterate over List<T> and render each item in JSF Facelets

JSP: Iterate over List and get each item (ForEach)

iterate over list with indexes but each index shoud represent single item

Pandas dataframe : Iterate over rows an item in column that contains list

How to iterate over Dict in Julia

How to iterate over a nested dictionary and check if a key exists from a list

python iterate over list and add special key based on 'new arrival'

How to iterate over multiple dictionary key list(value) pairs

How to iterate over list values in dictionary per key?

Cannot iterate over object created with key and value as a list

pandas iterate over many dataframe and create list of occurance per key

Iterate over intervals of a list

iterate over objects/list

Java Iterate Over List