Introduction

Lists are one of the most commonly used data structures in Python. At some point, you may encounter situations where you need to reduce a list to a single value. In Python, you can use the built-in function reduce() to perform this task. reduce() applies a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. In this tutorial, we will cover the basics of reducing a list using reduce() and explore some practical use cases. So let's get started!

Table of Contents :

  • Reducing Python list
    • Reducing list using loops
    • Reducing list using reduce() function 
  • Control flow of reduce function

Reducing Python list :

  • Sometimes we need to apply some logic to Python lists such that the final result is a single value.
  • e.g. Find the sum of all the elements of the list.
  • We can reduce a list in Python in following ways - 
    • Using loops
    • Using reduce() function

Reducing list using loops :

  • We can iterate over all the elements using a for loop or a while loop.
  • Apply our logic cumulatively to all the elements one by one.
  • We get a single value as we complete the list iterations.
  • return the single value as result.
  • Loops are slower than the reduce() function discussed ahead.
  • Code Sample : 

my_list = [10, 20, 30, 40, 50]
print("my_list = ", my_list)

sum = 0
for item in my_list:
    sum = sum + item

print("sum = ", sum)

# Output
# my_list =  [10, 20, 30, 40, 50]
# sum =  150


Reducing list using reduce() function :

  • We can use the reduce() function to reduce the list to a single value.
  • reduce() function belongs to a module functools.
  • To use the reduce function we'll have to import it from functools module :  from functools import reduce 
  • The syntax for reduce() function is :   single_value = reduce(logic_function, orig_list) 
  • Code Sample : 

from functools import reduce


def logic_function(item_1, item_2):
    sum = item_1 + item_2
    return sum


my_list = [10, 20, 30, 40, 50]
print("my_list = ", my_list)

sum = reduce(logic_function, my_list)
print("sum = ", sum)

# Output
# my_list =  [10, 20, 30, 40, 50]
# sum =  150

Control flow of reduce function :

  •  reduce()  function iterates over all the elements of the list.
  • the logic_function should take two parameters.
  • The elements of the list are passed to the logic_function cumulatively, from left to right. 
  • In simple words : 
    • first two elements are passed to the logic_function, 
    • the result of these two elements is then passed as argument to the logic_function along with the third element of the list.
    • this result is then passed as argument to the logic_function along with the fourth element of the list.
  • This process goes on till the whole list is iterated and we are left with a single value.
  • This single value is finally returned by the reduce()  function.
  • We can use lambda expression within the reduce()  function.
  • Code Sample : 

from functools import reduce

my_list = [10, 20, 30, 40, 50]
print("my_list = ", my_list)

sum = reduce(lambda x, y: x + y, my_list)
print("sum = ", sum)

# Output
# my_list =  [10, 20, 30, 40, 50]
# sum =  150


Prev. Tutorial : List comprehensions

Next Tutorial : Dictionaries