0

The current problem consists of the conversion of a dictionary to a list. I am unable to split the specific value of key and value pair into my desired results.

I have a dictionary that looks like this:

dict = [ {name:aa,age:12, id:121}, {name:bb,age:13, id:122},{name:cc,age:11, id:121}, {name:dd,age:15, id:122} ]

It has certain pairs of key and values and the 'ID' key is the most important of them.the ID value is repeated and so I am looking for lists of that value such that it would looks like this:

121 = [
{name:aa,age:12},
{name:cc,age:11}
]

122 = [
{name:bb,age:13},
{name:dd,age:15}
]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Kathan Vyas
  • 355
  • 3
  • 16
  • 1
    What have you tried? Are you aware that the output variable names are not valid? You'll probably want to use a dict instead. And in the input, there are multiple variable names not defined; are they supposed to be strings? – wjandrea Sep 18 '21 at 01:09
  • Could your output be a dictionary of lists, with 'ID' as the key values and your given lists as values? E.g., `answer = {121: [{name:aa, age:12}, {name:cc, age:11}]}` etc. ? Otherwise, how are the output lists and id's arranged? – kcsquared Sep 18 '21 at 01:09
  • Hey there! I think this answers your question perfectly: [here](https://stackoverflow.com/questions/1679384/converting-dictionary-to-list) and [here](https://stackoverflow.com/questions/674519/how-can-i-convert-a-dictionary-into-a-list-of-tuples). Hope I helped! – VintageMind Sep 18 '21 at 01:09
  • @kcsquared yes that is fine – Kathan Vyas Sep 18 '21 at 01:11
  • @VintageMind Those seem more about converting a dictionary to (key, value) pairs. This question is very similar, but not an exact duplicate of [this question](https://stackoverflow.com/questions/16176469/how-do-i-group-this-list-of-dicts-by-the-same-month); the summary is to sort the list of dicts by that value, followed by itertools.groupby() – kcsquared Sep 18 '21 at 01:15

2 Answers2

1

I think this should work fine, just looping through the list of sub-dictionaries.

start_dict = [{'name':'aa','age':12,'id':121}, {'name':'bb','age':13,'id':122},{'name':'cc','age':11,'id':121}, {'name':'dd','age':15,'id':122}]

converted_dict = {}
for subdict in start_dict:
    if subdict['id'] not in converted_dict:
        converted_dict[subdict['id']] = [{k:v for k,v in subdict.items() if k != 'id'}]
    else:
        converted_dict[subdict['id']].append({k:v for k,v in subdict.items() if k != 'id'})
        
print(converted_dict)

{121: [{'name': 'aa', 'age': 12}, {'name': 'cc', 'age': 11}], 
 122: [{'name': 'bb', 'age': 13}, {'name': 'dd', 'age': 15}]}
StevenS
  • 662
  • 2
  • 7
1
from collections import defaultdict

start_dict = [
    {'name': 'aa', 'age': 12, 'id': 121},
    {'name': 'bb', 'age': 13, 'id': 122},
    {'name': 'cc', 'age': 11, 'id': 121},
    {'name': 'dd', 'age': 15, 'id': 122},
]
new_dict = defaultdict(list)

for entry in start_dict:
    new_dict[entry["id"]].append(dict(name=entry["name"], age=entry["age"]))

print(dict(new_dict))
knowingpark
  • 639
  • 7
  • 16