-1
a =[{
    "id":"1",
    "Name":'BK',
    "Age":'56'
},
{
    "id":"1",
    "Sex":'Male'
},
{
    "id":"2",
    "Name":"AK",
    "Age":"32"
}]

I have a list of dictionary with a person information split in multiple dictionary as above for ex above id 1's information is contained in first 2 dictionary , how can i get an output of below

{1: {'Name':'BK','Age':'56','Sex':'Male'}, 2: { 'Name': 'AK','Age':'32'}}
sushanth
  • 8,275
  • 3
  • 17
  • 28
user1511808
  • 65
  • 1
  • 3
  • 7

5 Answers5

0

Using defaultdict

from collections import defaultdict

a = [{
    "id": "1",
    "Name": 'BK',
    "Age": '56'
},
    {
        "id": "1",
        "Sex": 'Male'
    },
    {
        "id": "2",
        "Name": "AK",
        "Age": "32"
    }
]

final_ = defaultdict(dict)
for row in a:
    final_[row.pop('id')].update(row)

print(final_)

defaultdict(<class 'dict'>, {'1': {'Name': 'BK', 'Age': '56', 'Sex': 'Male'}, '2': {'Name': 'AK', 'Age': '32'}})
sushanth
  • 8,275
  • 3
  • 17
  • 28
0

You can use a defaultdict to collect the results.

from collections import defaultdict

a =[{ "id":"1", "Name":'BK', "Age":'56' }, { "id":"1", "Sex":'Male' }, { "id":"2", "Name":"AK", "Age":"32" }]

results = defaultdict(dict)
key = lambda d: d['id']

for a_dict in a:
    results[a_dict.pop('id')].update(a_dict)

This gives you:

>>> results
defaultdict(<class 'dict'>, {'1': {'Name': 'BK', 'Age': '56', 'Sex': 'Male'}, '2': {'Name': 'AK', 'Age': '32'}})

The defaultdict type behaves like a normal dict, except that when you reference an unknown value, a default value is returned. This means that as the dicts in a are iterated over, the values (except for id) are updated onto either an existing dict, or an automatic newly created one.

How does collections.defaultdict work?

0
from collections import defaultdict
result = defaultdict(dict)
    
a =[{ "id":"1", "Name":'BK', "Age":'56' }, { "id":"1", "Sex":'Male' }, { "id":"2", "Name":"AK", "Age":"32" }]

for b in a:
  result[b['id']].update(b)

print(result)
BIlguun Zorigt
  • 425
  • 1
  • 9
  • 15
0

You can combine 2 dictionaries by using the .update() function

dict_a = { "id":"1", "Name":'BK', "Age":'56' }
dict_b = { "id":"1", "Sex":'Male' }
dict_a.update(dict_b) # {'Age': '56', 'Name': 'BK', 'Sex': 'Male', 'id': '1'}

Since the output the you want is in dictionary form

combined_dict = {}

for item in a:
  id = item.pop("id")  # pop() remove the id key from item and return the value
  
  if id in combined_dict:
    combined_dict[id].update(item)
  else:
    combined_dict[id] = item

print(combined_dict)  # {'1': {'Name': 'BK', 'Age': '56', 'Sex': 'Male'}, '2': {'Name': 'AK', 'Age': '32'}}
Thu Ya Kyaw
  • 144
  • 6
0
d = {}
for p in a:
    id = p["id"]
    if id not in d.keys():
        d[id] = p
    else:
        d[id] = {**d[id], **p}

d is the result dictionary you want.

In the for loop, if you encounter an id for the first time, you just store the incomplete value.

If the id is in the existing keys, update it.

The combination happens in {**d[id], **p}

where ** is unpacking the dict.

It unpacks the existing incomplete dict associated withe the id and the current dict, then combine them into a new dict.

Lee
  • 520
  • 5
  • 13
  • Hi @Lee can you pelase update your answer to include an explanation about how it works and how does is solve the question? – aaossa Mar 20 '22 at 19:29