Kaizer von Maanen
Kaizer von Maanen

Reputation: 987

Python elementwise addition from a dict

I have a dictionary in the form.

dictName = {'Hepp': [-1,0,1], 'Fork': [-1,-1,-1], 'Dings': [0,0,1]}

and I basically want to pull out the values ( the lists )

and add them together elementwise and get a vector as a result, like

[-2,-1,1]

I am having a hard time figuring out how to code this, and all examples I have found for adding lists assumes that I can make it into tuples, but I might have to add like 100 lists together. Can anyone of you guys help out?

Upvotes: 2

Views: 1682

Answers (1)

jh314
jh314

Reputation: 27792

You can use a list comprehension, and zip:

[sum(t) for t in zip(*dictName.itervalues())]

Upvotes: 5

Related Questions