Search code examples
pythonlist-comprehensiondictionary-comprehensionpython-jsonschema

Parse List in nested dictionary Python


data = {
    "persons": {"1": {"name": "siddu"}, "2": {"name": "manju"}},
    "cars": {
        "model1": {
            "make": 1990,
            "company_details": {
                "name": "Ford Corporation",
                "country": "US",
                "some_list": [1, 2, 1],
            },
        },
        "model2": {
            "make": 1990,
            "company_details": {
                "name": "Ford Corporation",
                "country": "US",
                "some_list": [1, 2, 1, 1, 1],
            },
        },
    },
}

This is my python object, How can I identify the Key's-Value is a list. example here, after traversing through 'print(data["cars"]["model1"]["company_details"]["some_list"])'I get the list, since it is small dictionary it was easy, but how can I identify the same if I encounter list as a value for some other key in future.

Example:

data = {
    "persons": {"1": {"name": "siddu"}, "2": {"name": "manju"}},
    "cars": {
        "model1": {
            "make": 1990,
            "company_details": {
                "name": "Ford Corporation",
                "country": "US",
                "some_list": [1, 2, 1],
            },
        },
        "model2": {
            "make": 1990,
            "company_details": {
                "name": "Ford Corporation",
                "country": ["US", "UK", "IND"],
                "some_list": [1, 2, 1, 1, 1],
            },
        },
    },
}

Can anyone please suggest/guide me to understand how to identify the key's value is a list. The final goal is to remove the duplicates in the list if any exists? Thank you very much:)


Solution

  • You can have a recursive function that goes to any depth and make the items of the list unique like below:

    In [8]: def removeDuplicatesFromList(di):
       ...:     for key, val in di.items():
       ...:         if isinstance(val, dict):
       ...:             removeDuplicatesFromList(val)
       ...:         elif isinstance(val, list):
       ...:             di[key] =list(set(val))
       ...:         else:
       ...:             continue
       ...:
       ...:
    In [9]: removeDuplicatesFromList(data)
    
    In [10]: data
    Out[10]:
    {'persons': {'1': {'name': 'siddu'}, '2': {'name': 'manju'}},
     'cars': {'model1': {'make': 1990,
       'company_details': {'name': 'Ford Corporation',
        'country': 'US',
        'some_list': [1, 2]}},
      'model2': {'make': 1990,
       'company_details': {'name': 'Ford Corporation',
        'country': 'US',
        'some_list': [1, 2]}}}}