How can I create a new nested dictionary from an old nested dictionary with updated keys and value?

NoobPythoner

Currently, I am trying to create a new dictionary from an unusable dictionary but am having difficulty updating/creating new keys based on the number of tuples in the 'Acceptable' key.

dirty_dictionary = {'DOG': {'Acceptable': ([[35, 38]], 'DOG_GROUP'),
                    'Unacceptable': ([[2], [29], [44], [50], [54], [60]], 'DOG_GROUP')},
                    'CAT': {'Acceptable': ([[3, 6], [100, 101]], 'CAT_GROUP'), 'Unacceptable': ([[12], [18], [45], [51], [61]], 'CAT_GROUP')},
                    'FISH': {'Acceptable': ([], 'FISH_GROUP'), 'Unacceptable': ([[13], [19], [22], [28], [34]], 'FISH_GROUP')},
                    'COW': {'Acceptable': ([[87, 69]], 'COW_GROUP'), 'Unacceptable': ([], 'COW_GROUP')}}

new_dict = {}
for key, values in dirty_dictionary.items():
    if len(values['Acceptable'][0]) == 0:
        new_dict[dirty_dictionary[key]['Acceptable'][-1]] = {'Acceptable': []}
    for oids in values['Acceptable'][0]:
        if len(values['Acceptable'][0]) == 1:
            new_dict[dirty_dictionary[key]['Acceptable'][-1]] = {'Acceptable': oids}
        if len(values['Acceptable'][0]) > 1:
            for i in range(len(values['Acceptable'][0])):
                new_dict[dirty_dictionary[key]['Acceptable'][-1] + F'_{i}'] = {'Acceptable': values['Acceptable'][0][i]}

    # for oids in values['Unacceptable'][0]:
    #     if len(values['Unacceptable'][0]) == 1:
    #         new_dict[dirty_dictionary[key]['Unacceptable'][-1]].update({'Unacceptable': oids})
    #     if len(values['Unacceptable'][0]) > 1:
    #         for i in range(len(values['Unacceptable'][0])):
    #             new_dict[dirty_dictionary[key]['Unacceptable'][-1] + F'_{i}'].update({'Unacceptable': values['Unacceptable'][0][i]})

print(new_dict)

I can create a new dictionary with all 'Acceptable' Keys/Values, but I am stuck on updating the dictionary with the 'Unacceptable' since new groups need to be created if the len of values['Acceptable'][0] > 1.

The goal is to get the final dictionary to look like:

final = {'DOG_GROUP': {'Acceptable': [35, 38], 'Unacceptable': [2, 29, 44, 50, 54, 60]},
         'CAT_GROUP_0': {'Acceptable': [3, 6], 'Unacceptable': []},
         'CAT_GROUP_1': {'Acceptable': [100, 101], 'Unacceptable': [12, 18, 45, 51, 61]},
         'FISH_GROUP': {'Acceptable': [], 'Unacceptable': [13, 19, 22, 28, 34]},
         'COW_GROUP': {'Acceptable': [87, 69], 'Unacceptable': []}}
Mazedul Islam

Try this one.

dirty_dictionary = {'DOG': {'Acceptable': ([[35, 38]], 'DOG_GROUP'), 'Unacceptable': ([[2], [29], [44], [50], [54], [60]], 'DOG_GROUP')}, 'CAT': {'Acceptable': ([[3, 6], [100, 101]], 'CAT_GROUP'), 'Unacceptable': ([[12], [18], [45], [51], [61]], 'CAT_GROUP')}, 'FISH': {'Acceptable': ([], 'FISH_GROUP'), 'Unacceptable': ([[13], [19], [22], [28], [34]], 'FISH_GROUP')}}

new_dict = {}

# assign text strings in variable to get suggestion in IDE
acceptable = 'Acceptable'
unacceptable = 'Unacceptable'

for key, values in dirty_dictionary.items():
    group = values[acceptable][1]
    if len(values[acceptable][0]) <= 1:
        new_dict[group] = {}
        new_dict[group][acceptable] = [y for x in values[acceptable][0] for y in x]
        new_dict[group][unacceptable] = [y for x in values[unacceptable][0] for y in x]
    else:
        for idx, item in enumerate(values[acceptable][0]):
            group_temp = group + '_' + str(idx+1)
            new_dict[group_temp] = {}
            new_dict[group_temp][acceptable] = item
            # if last item then give all unacceptable as a single array
            if idx == len(values[acceptable][0]) - 1:
                new_dict[group_temp][unacceptable] = [y for x in values[unacceptable][0] for y in x]
            else: # else empty array
                new_dict[group_temp][unacceptable] = []

print(new_dict)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how can I create nested dictionary keys and assign them values from a list of namespaced key value pairs?

How do I remove certain keys from a nested dictionary in Python?

How can i iterate and order nested list as a dictionary value in python?

How can I convert nested dictionary keys to strings?

How can I search for specific keys in this nested dictionary in Python?

How can I create a flat dictionary from a nested dictionary whose keys are a subset of a reference dictionary?

How can I remove a value in this list if it is not inside one of the keys of this nested dictionary?

How to build path from keys of nested dictionary?

Get keys with maximum value from a nested dictionary

how can I turn nested dictionary into flat dictionary and sum values with duplicate keys?

How can I create a nested dictionary using a for loop in Python?

How to create a dictionary from slices of nested dictionary?

How to create a nested dictionary

How to add a list of value and new keys to a nested dictionary?

How can I create columns out of a nested dictionary in python pandas

create dictionary of values based on matching keys in list from nested dictionary

Get average value of keys in a dictionary that are in a nested dictionary

Create nested dictionary from another nested dictionary

How can I get the key value pair to create a dictionary from a nested dictionary?

Create nested dictionary with same keys from a file

how do I get keys from a nested dictionary?

How can I change keys in a nested dictionary in Python

How to get number of keys for a nested value in a dictionary?

How to create a dictionary from a nested list of dictionary

How to create new dictionaries from nested dictionary?

how do I create dataframe from list of nested dictionary

how can I create a nested dictionary with list of dictionaries

create new json data from values in a dictionary and nested dictionary

How to create nested dataframe from nested dictionary?