How to add all int values in a dict

NekoTony

So, imagine I got a dict like this:

dict = {
    "100311929821626368" : {
        "battles" : 2,
        "loses" : 0,
        "maxstamina" : 50,
        "news" : True,
        "stamina" : 50,
        "weeb" : "Chuey",
        "wins" : 0
    },
    "100802430467325952" : {
        "battles" : 4,
        "loses" : 0,
        "maxstamina" : 50,
        "news" : True,
        "stamina" : 45,
        "weeb" : "Red Haired Bastard",
        "wins" : 0
    },
    "101970309296447488" : {
        "battles" : 1,
        "loses" : 0,
        "maxstamina" : 50,
        "news" : True,
        "stamina" : 45,
        "weeb" : "Niusky",
        "wins" : 1
    }
}

and my code is this code:

wow = 0
for id in dict:
   for i in dict[id]["battles"]:
      wow += i

The problem is, I'm not sure how to add all the battles int at once. Because I get this error:

TypeError: 'int' object is not iterable

How would i fix it so it all all the battles in the dict!

Julien

Is this what you want?

wow = 0
for id in dict:
     wow += dict[id]["battles"]

Explanation if necessary: dict[id]["battles"] already returns the int value. You were trying to iterate over that int which makes no sense.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python: How can one verify that dict1 is only a subset of dict2? Values are all int and within scope

How to add values from first dict to values in second dict

How to extract dict values and set as int variable?

How to add keys and values to a dict in a loop?

How to add all values in a HashMap?

how to convert all int to str in a nested dict with json.dumps()?

How to add two int array values together?

Python - how to get all the values of nested dictionaries as a single List[dict]?

How to check if all the item values of dict in python are same.?

How to generate all combination from values in dict of lists in Python

How to extract all values from an embedded dict/list

How to extract all data keys and values in a nested python dict

How to get all values from a list of dict that matches key prefix?

How to add a typing declaration for a dict with different types as values in python

If key already exists in a dict, how to add multiple values to it (Python)

JS - add multiple dict values into a dict key

Match dict values and add a key to the dict if it matchs

Put all values of dict in an array

Trying to get all values in a dict

Add pair of keys and values to dict

Add values to dict of list in Python?

How to use MapBuilder in kotlin and add all values?

In Pyspark how to add all values in a list?

How to add the values of all the fields in a struct in matlab?

How to add all values in one column in database

How to add all values from UnmodifiableSet to ArrayList?

How to add all comma separated values in XSLT

Python Dict create to own list and add all values with specific Keys in one line

How to add index into a dict