0

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.

Martijn Pieters
1.1m326 gold badges4.2k silver badges3.4k bronze badges
asked Apr 18, 2017 at 6:50
1
  • What have you tried so far and what problems have you run into? Commented Apr 18, 2017 at 7:14

2 Answers 2

1

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']
 }
 ]
 ]
}
answered Apr 18, 2017 at 7:40
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but, This also combines the next record into previous dictionary. For example, { "V-Su7890":[..................],''V-SZ86385ZM':[.............]} and the values in the 'email' contains duplicates.
@Niveram i edit my answer, put tmp={} in the for loop, and it won't combine next record.
Great.. Thanks McGrady.
Hi McGrady, My input is {u'V-WW62364WT':[[{..............},[.........], u'V-YS87486IT': its values,u'V-SZ86385ZM': its values} is there a possibility to split at the keys so then the above code can work perfectly.
0

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]
answered Apr 18, 2017 at 7:22

2 Comments

Sorry, I have a doubt. What is V = k["V-Su7890"][1] ? you mentioned as "store the complete dict in a variable V" but the file contains only lists of dictionaries as I have sampled above
You have enclosed the original the list of dict inside curly braces which itself is a dict. { "V-Su7890": [...]}

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.