Explain Codes LogoExplain Codes Logo

Nameerror: name 'reduce' is not defined in Python

python
functions
promises
collections
Anton ShumikhinbyAnton Shumikhin·Mar 1, 2025
TLDR

To squash the NameError for reduce, pull in reduce from the functools module like this:

from functools import reduce result = reduce(lambda x, y: x * y, [1, 2, 3, 4]) # Is it a coincidence that 24 is 2, 4? 🤔

This allows reduce to apply a function of two arguments cumulatively to the items of a sequence.

Why is reduce acting shy?

The reason behind disappearing act

Changes in Python 3 relocated reduce into the functools module. A little bit like moving away from the city's bustle into the quiet suburbs, isn't it? This was done due to the determination that reduce was more of a functional programming tool rather than a ubiquitous one. Hence, it’s required to be imported from functools explicitly - not unlike needing a special pass to access some suburban areas.

Cross-version compatibility tip

For those coding ninjas trying to maintain backward compatibility with Python 2 and 3, six.moves is your saving grace to maintain access to reduce across the Python ecosystem.

Alternatives to reduce

Existing equivalents

Fancy for alternative to reduce for adding elements linearly? Look no further! Try sum for these operations. It's like having a slice of cheese instead of the whole cake. You get similar results but in a simpler way.

total = sum([1, 2, 3, 4]) # Did you know this is also the sum of the first four Fibonacci numbers?

For cases where you need to apply a function other than addition, your trusty sidekick, list comprehension, comes to rescue! Just make sure you use sum with list comprehension as your arsenal.

Journey outside the safezone

Remember, sum is for addition, while reduce is inches ahead due to its ability to use any binary function. So choose your weapon wisely.

Beware of the lonely path

Just like how you can't reduce the contents of an empty bag, attempting to reduce on an empty sequence results in a snarky TypeError. To avoid this awkward situation, specifics a start value as a kickstarter to reduce.

from functools import reduce # Avoid TypeError with a start value result = reduce(lambda x, y: x + y, [], 0) # When life gives you lemons... 🍋

How to make reduce your ally

Simplifying reduce with operator

Who needs long lambda expressions when you can simplify your code with the operator module?

from functools import reduce import operator result = reduce(operator.mul, [1, 2, 3, 4]) # More readable than lambda x, y: x * y

The dynamic duo

In the Python world, reduce and map are like Batman and Robin. Together, they can solve more complex functional problems:

from functools import reduce result = reduce(lambda x, y: x * y, map(lambda z: z + 1, [1, 2, 3, 4])) # Increments then multiplies

Debugging with reduce

If you're like Sherlock Holmes and like to inspect every detail, you can see each step of the reduce process with print statements or the itertools functions:

from functools import reduce def debug_reduce_func(x, y): result = x * y print(f"Reducing: {x} * {y} = {result}") return result reduce_result = reduce(debug_reduce_func, [1, 2, 3, 4]) # Watch each step like a CSI investigator