-3

Problem

Input:

input_list = [(1, 2), (1, 4), (1, 6)]

Expected Output:

(3, 12)

I've tried

print(reduce(lambda a, b: (a[0] + b[0], a[1] + b[1]), input_list))

and

print(reduce(lambda (a, b), (c, d): (a + c, b + d), input_list))

both of which fail due to invalid syntax.

miradulo
  • 28,857
  • 6
  • 80
  • 93
David
  • 167
  • 3
  • 11
  • I think the question here has more to do with *why* an otherwise correct-looking solutoin contains a syntax error, rather than how to do pairwise addition of tuples. – chepner Apr 20 '18 at 20:09
  • @miradulo It seems to be a question about how to do this specifically with `reduce` and specifically with python 3, which suggests it was not related to the proposed duplicates, IMO anyway. – ely Apr 20 '18 at 20:11
  • Irrespective of whether they are more interested in why using a reserved keyword is a syntax error or summing tuples, this has just come up many, many times. – miradulo Apr 20 '18 at 20:12
  • An updated duplicate: [NameError: name 'reduce' is not defined in Python](https://stackoverflow.com/questions/8689184/nameerror-name-reduce-is-not-defined-in-python) – miradulo Apr 20 '18 at 20:17
  • There is *nothing* wrong with the syntax of your first try; what error are you *actually* seeing? – chepner Apr 20 '18 at 20:20
  • @ely I really don't understand how you're drawing that much from the question. And your answer goes no further than the answer on the duplicate. – miradulo Apr 20 '18 at 20:24
  • 1
    Let this be a lesson to (1) always run code before posting it (2) copy paste the full actual traceback. – Alex Hall Apr 20 '18 at 20:33
  • @ely I don't understand your reasoning for this not being a duplicate at all. If the OP (or anyone with this same issue) were to actually look up the error they were getting, that is literally the title of the duplicate question. Consequently, I don't see how this question or answer adds any value. Unless one were to happen to be summing tuples, not import `functools.reduce`, and not perform a proper search with their error. But anyhow, agree to disagree I guess. – miradulo Apr 20 '18 at 21:15
  • @miradulo I guess I see a difference between "why does this exact exception get raised" versus "why does this code that achieves what I want in Python 2 fail to achieve the result in Python 3, and what are a variety of possible solutions". To the latter question, one answer might be explaining about `functools.reduce`, even though the question is not principally about the specific phenomenon of plain `reduce` generating a `NameError` (that's just one part, the other being any other way to solve the same problem by modifying the Python 2 code to work in some other way in Python 3). – ely Apr 20 '18 at 23:33

1 Answers1

1

In Python 3, reduce is moved from builtins into functools, so you need from functools import reduce.

ely
  • 74,674
  • 34
  • 147
  • 228
  • 3
    That would produce a `NameError`, not a `SyntaxError`. – chepner Apr 20 '18 at 20:12
  • @chepner We never had confirmation that it truly was `SyntaxError`, only "fail due to invalid syntax" which could be a human interpretation of various types of errors. – ely Apr 20 '18 at 20:14
  • 1
    We also don't have any confirmation that the code isn't preceded by `from functools import reduce`. – chepner Apr 20 '18 at 20:14
  • @chepner But the connection to Python 3 gives some evidence that this is part of the problem, so my posterior beliefs about why this person is having the trouble would have some density on explanations which involve that factor. – ely Apr 20 '18 at 20:15
  • Thank you! This resolved the issue for the first style (using the square brackets), though it didn't resolve it for the second style. However, that's good enough for me. Side note: I find it odd how I was able to use reduce on a list of non-tuples without importing it. – David Apr 20 '18 at 20:28