1

It may be the fact I haven't slept yet, but I can't find the solution to this problem, so I come to you all. I have a list, with a series of sub-lists, each containing two values, like so:

list = (
  (2, 5),
  (-1, 4),
  ( 7, -3)
  )

I also have a variable, a similar list with two values, that is as such:

var = (0, 0)

I want to add all the x values in list, then all the y values, and then store the sums in var, so the desired value of var is:

var = (8, 6)

How could I do it? I apologize if the answer is something stupid simple, I just need to get this done before I can sleep.

2 Answers 2

6
sumvar = map(sum,zip(*my_list))

should do what you want i think

Sign up to request clarification or add additional context in comments.

Comments

0

This sounds like a job for "reduce" to me:

reduce(lambda a,b: (a[0]+b[0],a[1]+b[1]), list)
(8,6)

you could also use another list comprehension method, (a bit more readable):

sum(a for a,b in tpl), sum(b for a,b in tpl)
(8,6)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.