-10

I have a list list like this

values = [{'id':1, 'x':2}, {'id':1, 'y':4}, {'id':1, 'z':6}, {'id':2, 'j':5}, {'id':2, 'k':10}, {'id':3, 'w':1}, {'id':3, 'x':3}, {'id':3, 'y':5}, {'id':3, 'z':7}]

I want the result to be a List with new Dict values(in most efficient way, if possible) like this:

values = [{'id':1, 'x':2, 'y':4, 'z':6}, {'id':2, 'j':5, 'k':10}, {'id':3, 'w':1, 'x':3, 'y':5, 'z':7}]

How should Loop for that kind of case.

nhtgca
  • 11
  • 1

5 Answers5

2

Not beautiful solution, but working one

values = [{'id': 1, 'x': 2}, {'id': 1, 'y': 4}, {'id': 1, 'z': 6},
          {'id': 2, 'j': 5},
          {'id': 2, 'k': 10}, {'id': 3, 'w': 1}, {'id': 3, 'x': 3},
          {'id': 3, 'y': 5}, {'id': 3, 'z': 7}]

# get all unique possible keys
unique_keys = set((x['id'] for x in values))
result = []
for value in unique_keys:
    new_dict = dict(id=value)
    for old_dict in values:
        if old_dict['id'] == value:
            new_dict.update(old_dict)
    result.append(new_dict)
print(result)

The printed result:

{'id': 1, 'z': 6, 'x': 2, 'y': 4}, {'j': 5, 'id': 2, 'k': 10}, {'x': 3, 'y': 5, 'id': 3, 'w': 1, 'z': 7}]
Logovskii Dmitrii
  • 2,629
  • 4
  • 27
  • 44
1

Using a new syntax proposed in PEP 448 and available as of Python 3.5

from itertools import groupby
from operator import itemgetter

groups = groupby(values, itemgetter('id'))
new_list = []
for k,g in groups:
    z = {}
    for x in g:
        z = {**z,**x}
    new_list.append(z)
print(new_list)
Ketan Mukadam
  • 789
  • 3
  • 7
0

A crude solution:

>>> from itertools import groupby
>>> values = [{'id':1, 'x':2}, {'id':1, 'y':4}, {'id':1, 'z':6}, {'id':2, 'j':5},
 {'id':2, 'k':10}, {'id':3, 'w':1}, {'id':3, 'x':3}, {'id':3, 'y':5}, {'id':3, 'z':7}]
>>> new_values = []
>>> for g,x in groupby(sorted(','.join('{}|{}'.format(k,v) for k,v in sorted(d.items())) for d in values),lambda x: x[:x.index(',',3)]):
        nd = dict([g.split('|')])
        nd.update(z[len(g)+1:].split('|') for z in x)
        new_values.append(nd)

>>> new_values
[{'y': '4', 'x': '2', 'z': '6', 'id': '1'}, {'k': '10', 'j': '5', 'id': '2'}, {'y': '5', 'x': '3', 'z': '7', 'id': '3', 'w': '1'}]
mshsayem
  • 17,557
  • 11
  • 61
  • 69
0

A get by solution with collections.defaultdict:

from collections import defaultdict

values = [{'id':1, 'x':2}, {'id':1, 'y':4}, {'id':1, 'z':6}, {'id':2, 'j':5},
          {'id':2, 'k':10}, {'id':3, 'w':1}, {'id':3, 'x':3}, {'id':3, 'y':5}, {'id':3, 'z':7}]

d = defaultdict(list)
for val in values:
    d[val['id']].append(val)

result = []
for key in d:
    temp = {}
    for x in d[key]:
        temp.update(x)

    result.append(temp)

print(result)

Which Outputs:

[{'id': 1, 'x': 2, 'y': 4, 'z': 6}, {'id': 2, 'j': 5, 'k': 10}, {'id': 3, 'w': 1, 'x': 3, 'y': 5, 'z': 7}]
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
0

Here is my approach:

values = [{'id':1, 'x':2}, {'id':1, 'y':4}, {'id':1, 'z':6}, {'id':2, 'j':5},
 {'id':2, 'k':10}, {'id':3, 'w':1}, {'id':3, 'x':3}, {'id':3, 'y':5}, {'id':3, 'z':7}]

dict_1={}

for i in values:
    if i['id'] not in dict_1:

        dict_1[i['id']]=[{k:l} for k,l in i.items()]
    else:
        dict_1[i['id']].append({k:l for k,l in i.items() if k!='id'})
print(dict_1)

output:

{1: [{'id': 1}, {'x': 2}, {'y': 4}, {'z': 6}], 2: [{'j': 5}, {'id': 2}, {'k': 10}], 3: [{'w': 1}, {'id': 3}, {'x': 3}, {'y': 5}, {'z': 7}]}