Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with dictionaries
#1
Hello everyone! I have looked for a way to sum up the elements of each value which are lists but couldn't find a solution. I'm curious about if there is a way to reach the elements in the value lists. Can you help me with that?

b = {"a":[0,5,3,-5], "b":[5,5,5,0], "c":[1,5,5,0]}
Actually this is what I am trying to get as output:

b = {"a":[3],"b":[15],"c":[11]}
Reply
#2
b = {"a":[0,5,3,-5], "b":[5,5,5,0], "c":[1,5,5,0]}
sums = {key: sum(value) for key, value in b.items()}
print(sums)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020