How To Resolve NameError: Name ‘Reduce’ Is Not Defined In Python

NameError: name 'reduce' is not defined in Python

To fix NameError: name ‘reduce’ is not defined in Python, we can use the functools module and the six.moves module. Let read the article below to find the cause and how to fix it.

What causes the NameError: name ‘reduce’ is not defined in Python?

‘NameError’ is an error when you use a variable, function, or module that is not declared or you are not using it properly.

“NameError: name ‘reduce’ is not defined” happens when you use the reduce() function, it has been removed from the built-in functions in Python 3 and moved to the ‘functools’ module.

Functools make higher-order functions work on top of other functions. The Functools module provides functions to work with other functions, and the objects in the module can be called for immediate use or extended without having to write entirely new.

The reduce() function is defined in the functools module.

Syntax:

reduce( <func>, iterable[, initializer])

Parameters:

  • <func>: Use a function to accumulate the elements of iterable (from left to right).
  • iterable: Iterable objects like lists and tuples.
  • initializer: Optional.

The return value of the reduce() function is a single cumulative value.

Example:

def sum(a, b):
    return a + b

myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sumOfList = reduce(sum, myList)

print('Sum of list :', sumOfList)

Output:

Traceback (most recent call last):
  File "./prog.py", line 5, in <module>
NameError: name 'reduce' is not defined

How to solve this error?

Import reduce from functools python package

Example:

  • Import reduce from functools Python package (this is an important step you need to remember as it is the cause of this error).
  • Declare the sum function.
  • Initialize the reduce() function to calculate the sum of that list.
# Import reduce from functools
from functools import reduce

def sum(a, b):
    return a + b

myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Use the reduce() function to calculate the sum of that list
sumOfList = reduce(sum, myList)

print('Sum of list :', sumOfList)

Output:

Sum of list : 45

Import reduce from six.moves module

Six packages provide utilities to cover the differences between Python 2 and Python 3. Six packages can support codebases that work on Python 2 and Python 3 without modification.

In addition to the functools module, the reduce() function is also defined in the ‘six.moves’ packages.

Example:

  • Import reduce from six.moves module.
  • Declare the sum function.
  • Initialize the reduce() function to calculate the sum of that list.
# Import reduce from six.moves module
from six.moves import reduce

def sum(a, b):
    return a + b

myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Use the reduce() function to calculate the sum of that list
sumOfList = reduce(sum, myList)

print('Sum of list :', sumOfList)

Output:

Sum of list : 45

Summary

I have shown the causes and given the remedies that fixed the NameError: name ‘reduce’ is not defined in Python. This is a pretty simple bug in Python. I hope you can fix it as soon as possible.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *