python how do i build a dictionary from a list of dictionaries?

lst = [
    'manufacurer': 'VW',
    'group': 'Passat',
    'age': 2
    'manufacurer': 'Audi',
    'group': 'Quadro',
    'age': 0.5
    'manufacurer': 'VW',
    'group': 'Golf',
    'age': 6,

out = {}
for item in lst:
    out.setdefault(item['manufacurer'], {})['manufacurer'] = item['manufacurer']
    out[item['manufacurer']].setdefault('value_list', []).append(item)

out = list(out.values())

# pretty print the list:    
from pprint import pprint


[{'manufacurer': 'VW',
  'value_list': [{'age': 2, 'group': 'Passat', 'manufacurer': 'VW'},
                 {'age': 6, 'group': 'Golf', 'manufacurer': 'VW'}]},
 {'manufacurer': 'Audi',
  'value_list': [{'age': 0.5, 'group': 'Quadro', 'manufacurer': 'Audi'}]}]

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top