Home > Software engineering >  Appending to nested list in python dictionary
Appending to nested list in python dictionary

Time:01-19

I have a dictionary with a nested list as values (actually thousands of entries, here just a simplification).

my_dict = {'topic a': [['CategoryA', 230], ['CategoryB', 270]], 'topic b': [['CategoryA', 198], ['CategoryB', 188]]}

I further have two lists with float values (ordered and same amount as list values): list_A: [0.1, 0.1], list_B: [0.2, 0.2]

I wish to iterate over the dictionary and lists and per iteration step append the float values of A to the first nested list of the dictionary's item and the float values of B to the second nested list of the dictionary's item. The order has to remain as it is.

My desired outcome is as follows:

{'topic a': [['CategoryA', 230, 0.1], ['CategoryB', 270, 0.2]], 'topic b': [['CategoryA', 198, 0.1], ['CategoryB', 188, 0.2]]}

I have tried so many things; the iteration just won't quite work. Please see the code of one of my attempts:

my_dict = {'topic a': [['CategoryA', 230], ['CategoryB', 270]], 'topic b': [['CategoryA', 198], ['CategoryB', 188]]}

list_A = [0.1, 0.1]
list_B = [0.2, 0.2]

for element in my_dict.values():
        for el in list_A:
            element[0].append(el) 
        for el_ in list_B:
            element[1].append(el_)
print(my_dict)

The undesired outcome is:

{'topic a': [['CategoryA', 230, 0.1, 0.1], ['CategoryB', 270, 0.2, 0.2]], 'topic b': [['CategoryA', 198, 0.1, 0.1], ['CategoryB', 188, 0.2, 0.2]]}

I also tried a workaround with zip, also no success it says 'TypeError: 'list' object is not callable' when trying to convert zip to list.

test = []
for element in my_dict.values():
    test = zip(element[0], list_A)
test = list(test)
print(test)

For my first example I actually understand why I get the undesired outcome but just can't get to the desired one; for the zip attempt I do not see the problem. Any (pythonic) ideas? - Very much appreciated!

Update: Dictionary transformed to list, still not working; what am I missing?



my_list = []
for key, value in my_dict.items():
    temp = [key,value]
    my_list.append(temp)
print(my_list)  

# output: [['topic a', [['CategoryA', 230], ['CategoryB', 270]]], ['topic b', [['CategoryA', 198], ['CategoryB', 188]]]]


list_A = [0.1, 0.1]
list_B = [0.2, 0.2]


for element in my_list:
        for el in list_A:
            element[1][0].append(el) 
        for el_ in list_B:
            element[1][1].append(el_)
print(my_list)

# output: [['topic a', [['CategoryA', 230, 0.1, 0.1], ['CategoryB', 270, 0.2, 0.2]]], ['topic b', [['CategoryA', 198, 0.1, 0.1], ['CategoryB', 188, 0.2, 0.2]]]]


test = []
for element in my_list:
    test = zip(element[1][0], list_A)
test = list(test)
print(test)

# output: TypeError: 'list' object is not callable

CodePudding user response:

Of course, since there are infinitely many interpretations of your request, I don't think this is actually what you want, but let's start from here:

my_dict = {'topic a': [['CategoryA', 230], ['CategoryB', 270]],
           'topic b': [['CategoryA', 198], ['CategoryB', 188]]}

list_A = [0.1, 0.1]
list_B = [0.2, 0.2]

desired = {'topic a': [['CategoryA', 230, 0.1], ['CategoryB', 270, 0.2]],
           'topic b': [['CategoryA', 198, 0.1], ['CategoryB', 188, 0.2]]}

for outerlist, column in zip(my_dict.values(), zip(list_A, list_B)):
    for nestedlist, floatvalue in zip(outerlist, column):
        nestedlist.append(floatvalue)

assert my_dict == desired

CodePudding user response:

This will work:

dict_keys = list(my_dict.keys())

for i in range(0, len(list_A)):
    val_A = list_A[i]
    val_B = list_B[i]

    my_dict[dict_keys[i]][0].append(val_A)
    my_dict[dict_keys[i]][1].append(val_B)

print(my_dict)
# {'topic a': [['CategoryA', 230, 0.1], ['CategoryB', 270, 0.2]], 'topic b': [['CategoryA', 198, 0.1], ['CategoryB', 188, 0.2]]}
  •  Tags:  
  • Related