NameError: name 'reduce' is not defined in Python

Sergey picture Sergey · Dec 31, 2011 · Viewed 127.7k times · Source

I'm using Python 3.2. Tried this:

xor = lambda x,y: (x+y)%2
l = reduce(xor, [1,2,3,4])

And got the following error:

l = reduce(xor, [1,2,3,4])
NameError: name 'reduce' is not defined

Tried printing reduce into interactive console - got this error:

NameError: name 'reduce' is not defined


Is reduce really removed in Python 3.2? If that's the case, what's the alternative?

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Dec 31, 2011

It was moved to functools.