Below is the sample input from a huge file of similar inputs.
{
"V-Su7890": [
[
{
"url": "www.talent.com",
"tid": "V-Su7890",
"id": "58ff787ffbad487b2c",
"company_name": "Talent Ltd"
}
],
[
{
"socials": ["facebook", "linkedin", "twitter"],
"title": "title of the pag",
"contact": ["+9563802140"],
"email": "email_id1"
},
{
"socials": ["facebook", "twitter", "linkedin"],
"title": "next title of the page",
"contact": ["+919765983442"],
"email": "email_id2"
}
]
]
}
I have to merge all subdictionaries of the second list of the current dictionary into one dictionary without duplicate values and then store the dictionary as a value to the key "V-Su7890".
The desired output is :
{
"V-Su7890": [
[
{
"url": "www.talent.com",
"tid": "V-Su7890",
"id": "58ff787ffbad487b2c",
"company_name": "Talent Ltd"
}
],
[
{
"socials": ["facebook", "linkedin", "twitter"],
"title": ["title of the pag", "next title of the page"],
"contact": ["+9563802140", "+919765983442"],
"email": ["email_id","email_id2"]
}
]
]
}
Kindly help me understand and solve this.
-
What have you tried so far and what problems have you run into?pvg– pvg2017年04月18日 07:14:09 +00:00Commented Apr 18, 2017 at 7:14
2 Answers 2
You can use setdefault() to insert key with a value of default (here you can use empty list), and extend the list if the new item doesn't exist.
for k,v in a.items():
tmp={}
for i in v[1]:
for k1,v2 in i.items():
if isinstance(v2,list):
tmp.setdefault(k1,[]).extend(i for i in v2 if i not in tmp[k1])
else:
tmp.setdefault(k1,[]).append(v2)
a[k]=[v[0],[tmp]]
print(a)
Result:
{
'V-Su7890': [
...
[
{
'contact': ['+9563802140','+919765983442'],
'socials': ['facebook','linkedin','twitter'],
'email': ['email_id1','email_id2'],
'title': ['title of the pag','next title of the page']
}
]
]
}
4 Comments
tmp={} in the for loop, and it won't combine next record.Let's assume you store the complete dict in a variable V. We store values for socials, title etc in a set to avoid duplicate values. Later on, we will convert the sets to list. Here's the solution:
V = k["V-Su7890"][1]
new_dict = {}
for v in V:
for key, value in v.iteritems():
if not new_dict.get(key, None):
new_dict[key] = set()
if isinstance(value, list):
for val in value:
new_dict[key].add(val)
else:
new_dict[key].add(value)
# Converting the sets to list
for key, value in new_dict.iteritems():
new_dict[key] = list(value)
k["V-Su7890"][1] = [new_dict]