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.

1
  • What have you tried so far and what problems have you run into?
    – pvg
    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']
      }
    ]
  ]
}
4
  • 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
    Commented Apr 18, 2017 at 7:54
  • @Niveram i edit my answer, put tmp={} in the for loop, and it won't combine next record.
    – McGrady
    Commented Apr 18, 2017 at 8:01
  • Great.. Thanks McGrady.
    – Niveram
    Commented Apr 18, 2017 at 8:12
  • 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.
    – Niveram
    Commented Apr 19, 2017 at 6:56
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]
2
  • 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
    – Niveram
    Commented Apr 18, 2017 at 7:52
  • You have enclosed the original the list of dict inside curly braces which itself is a dict. { "V-Su7890": [...]}
    – Charul
    Commented Apr 18, 2017 at 11:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.