0

dictionary

hi i want to sum value of the dic that is list d = {"a": [1,2,2,3,6,] , "b":[8,5,6,1]} i want to sum for example 1 + 2 + 3 + 6 for key a

7th Jun 2020, 1:14 PM
nima rasi
4 Answers
+ 2
sum(d["a"])
7th Jun 2020, 1:24 PM
Seb TheS
Seb TheS - avatar
+ 1
Make a `set` object from the `list` (dictionary value) to have unique values only, then sum the values in the `set`. d = { "a" : [1, 2, 2, 3, 6,] , "b" : [8, 5, 6, 1] } for key in d: print(sum(set(d[key])))
7th Jun 2020, 1:44 PM
Ipang
0
Thanks Seb TheS
7th Jun 2020, 1:41 PM
nima rasi