Is it possible to loop to sum values in lists within nested dictionaries?

python sum dictionary values by key
python get all values from nested dictionary
python sum nested dictionary
nested dictionary comprehension python
nested dictionary python
convert nested dictionary to list python
update nested dictionary python
sum specific values in dictionary python

my data structure is the following:

all= {
        'part1': {'act1': [0, 0], 'act2': [100, 0]},
        'part2': {'act1': [25, 1], 'act2': [100, 1]}, 
        'part3': {'act1': [25, 0], 'act2': [0, 1]}, 
        'part4': {'act1': [0, 0], 'act2': [0, 1]}
        }

My goal is to get the global sum of all the first items in the lists.

So far I have only managed to get the sum for each key of the top level dictionary, with this code:

for partData in all.values():
    depPart = 0
    for act in partData.keys():
        depPart += partData[act][0]   
    print(depPart)

It returns

100
125
25
0

while I would like to get the sum i.e. 250

Every attempt I have made to loop further in order to get this sum has resulted in errors.

Is looping the way to do it, if yes how? Or is there another / a better way to get to it? Thanks

The reason is that firstly

for partData in all.values():
    depPart = 0
    for act in partData.keys():
        depPart += partData[act][0]   
    print(depPart)

on line 2 depPart=0 each time you get a value from all print(depPart) prints value everytime it sums every value inside each value so your code should have been

depPart = 0
for partData in all.values():
    for act in partData.keys():
        depPart += partData[act][0]   
print(depPart)

hope it helps

Python, Python - Convert Lists to Nested Dictionary · Python | Remove duplicate dictionaries from nested dictionary · Python | Safe access nested dictionary keys​  Assuming you want your dictionaries in a list, use the following code. Two for loops are required: the first iterates through foods which is a list of dictionaries and the second iterates through the values in the dictionary that's currently being iterated over. food.values() yields the lists in your dictionary and lst[-1] selects the last

You can use the sum function with a generator expression that iterates through the values of the dict and the sub-dicts:

sum(n for d in a.values() for n, _ in d.values())

This returns:

250

Note that all is a built-in function name, so I've renamed the all variable in your question to a instead.

Python, Python | Convert flattened dictionary into nested dictionary · Python - Inner Nested Value List Mean in Dictionary · Python | Sum values for each key in nested  My code reads the lines and turns it into a nested dictionary. Currently, the code seems to be looping and overwriting the key values of the inner dictionary. Here is the file it's reading from: 1 assignment_1 85 100 0.25 2 test_1 90 100 0.25 3 exam_1 95 100 0.5

You can use a list comprehension with sum:

d = {'part1': {'act1': [0, 0], 'act2': [100, 0]}, 'part2': {'act1': [25, 1], 'act2': [100, 1]}, 'part3': {'act1': [25, 0], 'act2': [0, 1]}, 'part4': {'act1': [0, 0], 'act2': [0, 1]}}
_sum = sum(sum(a for a, *_ in c.values()) for c in d.values())

Output:

#[100, 125, 25, 0]
250

Python Nested Dictionary, Learn to create a Nested Dictionary in Python, access change add and remove nested dictionary items, iterate through a nested dictionary and more. A nested dictionary is created the same way a normal dictionary is created. zip() function​, to combine separate lists of keys and values obtained dynamically at runtime. In Python, all the elements of a list don't have to be the same type. That means it is possible to have a list like this: ['Milos', 'Jones', 48, 'male', 'smoker', 210] which represents one person giving personal information. (The last number is total cholesterol in mg/dL.) Then we could have a list of elements like this to represent a list of

A Primer on Scientific Programming with Python, First, we load all the lines of the file into a list of strings called lines. After the for line in lines[1:] loop, we have a dictionary data of dictionaries where all the property values are as the list data[p]. values() and simply send this list to Python's sum function and 6.1 Illustration of the nested dictionary 6.1 Dictionaries 345. In Python, a nested dictionary is a dictionary inside a dictionary. It's a collection of dictionaries into one single dictionary. nested_dict = { 'dictA': {'key_1': 'value_1'}, 'dictB': {'key_2': 'value_2'}} Here, the nested_dict is a nested dictionary with the dictionary dictA and dictB. They are two dictionary each having own key and value.

Introduction to Python Programming, Use for loop to read and assign student details to name, registration_number, and The student_ name dictionary is built by having name variable value as the key, number and total_marks variables are associated as list values to the key ➆. The use of dictionaries within dictionaries is called nesting of dictionaries. Something resembling the example popped up for me when i repeated the second step for all the variables. Writing ‘students’ as a list solves the immediate problem, but if you do it like this, you can see how it is possible to use dictionaries inside of dictionaries (at least I think that’s what happened?).

Python Nested Dictionary (With Examples), They are two dictionary each having own key and value. Create a Nested Dictionary. We're going to create dictionary of people within a dictionary. Example 1:  Well, if, for some reason, you are forced to use a nested ‘for’ instead of a single liner, it must be a bad condition, indeed. I assume that you want to add the elements of each list (say list1, list2), one by one, and the result to be another lis

Comments
  • There may be some error while copying code . I personally ran the code and got answer of 250 only
  • It does, thank you! And it uses loops to do it, so good to know it is possible to do so.
  • Well no problem
  • Yes, my bad, I did not see at first that the print line was deindented. Thanks again.
  • Thanks a lot, it works perfect. I'm beginning with python and i am not yet familiar with the sum function, I will further dig into it. Regarding the use of "all", duly noted, ty.
  • @MadPhysicist This solution does not use zip. Can you clarify?
  • I'm on mobile and the first draft said zip. I see you've edited since then.
  • Thanks, it works as well as blhsing's answer, so now I know two ways of doing it ;-)