manosbar
manosbar

Reputation: 316

Sum specific items from dictionary

I have a dictionary named dict with 50 items but -unfortunately- names do not have a specific pattern.

I want to get the sum of specific variables and do some checks:

dict[random_name_sum] == dict['random_name_1'] + dict['random_name_2'] + dict['random_name_3']

but the following error appears:

TypeError: unsupported operand type(s) for +: 'int' and 'list'

I searched for similar questions, but none of them can solve my problem, and the only solution I can think of is changing the data structure that may require some future manual actuons. I would like to avoid that. Do you have any ideas?

Upvotes: 0

Views: 103

Answers (1)

Jisu Hong
Jisu Hong

Reputation: 724

First check if the return type is int for the dict values because it seems like you have a list.

Upvotes: 1

Related Questions