Skip to content
Advertisement

How to delete values in a dict based on the presence of child values

I have a dict that looks like this:

OrderedDict([('arguments', {'index': 1, 'parent': None}), ('controls', {'index': 2, 'parent': None}), ('examples', {'index': 3, 'parent': None}), ('journal', {'index': 4, 'parent': None}), ('journal/config', {'index': 5, 'parent': 'journal'}), ('journal/journaltest', {'index': 6, 'parent': 'journal'}), ('procs', {'index': 7, 'parent': None}), ('processor', {'index': 8, 'parent': None}), ('prediction', {'index': 9, 'parent': None}), ('reports', {'index': 10, 'parent': None}), ('tooki', {'index': 11, 'parent': None}), ('tooki/help', {'index': 12, 'parent': 'tooki'}), ('tooki/lime', {'index': 13, 'parent': 'tooki'}), ('tooki/medium', {'index': 14, 'parent': 'tooki'}), ('tooki/share', {'index': 15, 'parent': 'tooki'}), ('tooki/trigger', {'index': 16, 'parent': 'tooki'})])

How can I iterate over the elements of this dict, and exclude any element that has “‘parent’ : None” and no child element (meaning, no other element in this dict has this one as a parent).

Expected output:

OrderedDict([('journal', {'index': 4, 'parent': None}), ('journal/config', {'index': 5, 'parent': 'journal'}), ('journal/journaltest', {'index': 6, 'parent': 'journal'}), ('tooki', {'index': 11, 'parent': None}), ('tooki/help', {'index': 12, 'parent': 'tooki'}), ('tooki/lime', {'index': 13, 'parent': 'tooki'}), ('tooki/medium', {'index': 14, 'parent': 'tooki'}), ('tooki/share', {'index': 15, 'parent': 'tooki'}), ('tooki/trigger', {'index': 16, 'parent': 'tooki'})])

Advertisement

Answer

I think this function can help you

def orderedDict(data_list: list):
    result: list = []
    for data in data_list:
        is_ok = True
        if data[1]["parent"] is None:
            is_ok = False
            for item in [x[0] for x in [y for y in data_list if y[0] != data[0]]]:
                if data[0]+"/" in item:
                    is_ok = True
                    break
        if is_ok:
            result.append(data)
    return result

To use it:

if __name__ == '__main__':
    data = ([('arguments', {'index': 1, 'parent': None}), ('controls', {'index': 2, 'parent': None}),
             ('examples', {'index': 3, 'parent': None}), ('journal', {'index': 4, 'parent': None}),
             ('journal/config', {'index': 5, 'parent': 'journal'}),
             ('journal/journaltest', {'index': 6, 'parent': 'journal'}), ('procs', {'index': 7, 'parent': None}),
             ('processor', {'index': 8, 'parent': None}), ('prediction', {'index': 9, 'parent': None}),
             ('reports', {'index': 10, 'parent': None}), ('tooki', {'index': 11, 'parent': None}),
             ('tooki/help', {'index': 12, 'parent': 'tooki'}), ('tooki/lime', {'index': 13, 'parent': 'tooki'}),
             ('tooki/medium', {'index': 14, 'parent': 'tooki'}),
             ('tooki/share', {'index': 15, 'parent': 'tooki'}),
             ('tooki/trigger', {'index': 16, 'parent': 'tooki'})])
    result = orderedDict(data_list=data)
    print(result)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement