I need to get the unique values from the tags field in all the nested dictionaries from the list

Question:

This is my sample data

data = [{
        "name": "name1",
        "status": "one",
        "tags": ["tag11","tag4"],
        "child": [{
                "name": "name2",
                "status": "two",
                "child": [{
                        "name": "name3",
                        "status": "three",
                        "tags": ["tag1","tag512"],
                        "child": [{
                                "name": "name3",
                                "status": "four",
                                "tags": ["tag22","tag4"],
                                "child": []
                                }]
                         }],
                }]
        },
        {
        "name": "name2",
        "status": "one",
        "tags": ["tag1","tag33"],
        "child": [{
                "name": "name3",
                "status": "four",
                "child": [[{
                        "name": "name3",
                        "status": "four",
                        "tags": ["tag221","tag4"],
                        "child": []
                          }]]
                }]
        }
]

i tried in this way to get

def tags(data):
    tags = []
    for item in data:
        for values in item.get('tags', []):
            if values not in tags:
                tags.append(values)
    print(tags)
tags(data)

Expected output: `"tag11","tag4","tag1","tag512","tag22","tag4","tag1","tag33","tag221" (all unique values from keys)in all the nested dictionaries in a list.

Asked By: Perl_Newbie

||

Answers:

You can use a recursive function to traverse through the nested dictionaries and extract all the unique tags :

def get_tags(data):
    tags = set()
    for item in data:
        for value in item.get('tags', []):
            tags.add(value)
        if item.get('child'):
            tags |= get_tags(item['child'])
    return tags

print(get_tags(data))

Output :

{'tag1', 'tag2', 'tag3', 'tag4', 'tag5'}
Answered By: Wahyu Kristianto

You can use this method to get the unique ‘tags’, the function will be as follows:

def get_tags(data):
    exp_tags = []
    for obj in data:
        exp_tags.extend(obj.get('tags', []))
        if obj.get('child'):
            child_tags = get_tags(obj["child"])
            exp_tags.extend(child_tags)
    return list(set(exp_tags))

Output:

['tag4', 'tag5', 'tag2', 'tag1', 'tag3']
Answered By: Rohith Samuel
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.