duddal
duddal

Reputation: 19

Appending values to a dict in a dictionary python

I have a dictionary of dictionaries as follows:

test_dict = {
 'A': {'Total': 0, '0-20': 0, '20-40': 0,'40-60': 0,'60-80': 0,'80-100': 0},

 'B': {'Total': 0, '0-20': 0, '20-40': 0,'40-60': 0,'60-80': 0,'80-100': 0}
}

I have a list of tuples like following:

values = [(25.43246432903626, 4),
 (31.90641345733643, 4),
 (55.4526475309197, 4),
 (84.13675556557858, 4),
 (25.026203812005424, 4),
 (34.46739945421961, 4),
 (60.26098508606957, 4),
 (26.270296485819014, 4),
 (33.40999326977421, 4),
 (61.37002681032798, 4)]

What I want to do is loop through this values list and append the dictionary as follows: Loop through the value list and :-

  1. if the value[idx][1] is 4 then update the values of 'A' dict in test_dict
  2. if the value[idx][1] is 6 then update the values of 'B' dict in test_dict

For this I have written a function called:

Edit:

 def update_objects_new(idx, test_dict, obj):
    if 0 < idx < 20:
        test_dict[obj]['0-20'] += 1
    if 20 < idx < 40:
        test_dict[obj]['20-40'] += 1
    if 40 < idx < 60:
        test_dict[obj]['40-60'] += 1
    if 60 < idx < 80:
        test_dict[obj]['60-80'] += 1
    if 80 < idx < 100:
        test_dict[obj]['80-100'] += 1

    return test_dict

I tried doing this multiple ways but both the dictionaries in test_dict are updated even though value[idx][1] doesn't have 6. So I tried the following:

    for idx in values:
        if idx[1] == 4:
            update_objects_new(idx[0], test_dict, 'A')
        elif idx[1] == 6:
            update_objects_new(idx[0], test_dict, 'B')

However, the result is not what I am expecting. Here, because there isn't any 6 in the values list, the values of the keys in test_dict['B'] should remain 0 but they too are getting updated.

Expected Output:

test_dict = {
 'A': {'Total': 0, '0-20': 0, '20-40': 6,'40-60': 1,'60-80': 2,'80-100': 1},

 'B': {'Total': 0, '0-20': 0, '20-40': 0,'40-60': 0,'60-80': 0,'80-100': 0}
}

Any suggestions?

Thank You.

Upvotes: 1

Views: 98

Answers (2)

gimix
gimix

Reputation: 3833

Although the code by deadshot is more concise, I just copy-pasted your code in an IDLE windows and it works. The only reason I can imagine for the issue you describe is that the two sub-dicts were actually the same object - may be you created them in a function with an empty dict as parameter?

Upvotes: 1

deadshot
deadshot

Reputation: 9071

You can create a dictionary like below to find the key in your dictionary

dict_idx = dict(zip(range(5), ('0-20', '20-40', '40-60', '60-80', '80-100')))

Then update the values using the for loop

for a, b in values:
    if b == 4:
        test_dict['A'][dict_idx[int(a/20)]] += 1
    if b == 6:
        test_dict['B'][dict_idx[int(a/20)]] += 1
print(test_dict)

Output:

{'A': {'Total': 0, '0-20': 0, '20-40': 6, '40-60': 1, '60-80': 2, '80-100': 1},
 'B': {'Total': 0, '0-20': 0, '20-40': 0, '40-60': 0, '60-80': 0, '80-100': 0}}

Upvotes: 1

Related Questions