How to use Python reduce()

Python provides 3 useful global functions we can use to work with collections: map(), filter() and reduce().

Tip: sometimes list comprehensions make more sense and are generally considered more pythonic

reduce() is used to calculate a value out of a sequence, like a list.

For example suppose you have a list of expenses, stored as tuples, and you want to calculate the sum of a property property in each tuple, in this case the cost of each expense:

expenses = [
    ('Dinner', 80),
    ('Car repair', 120)
]

You could iterate with a loop over them:

sum = 0
for expense in expenses:
    sum += expense[1]

print(sum) # 200

Or, you can use reduce() to reduce the list to a single value:

from functools import reduce

print(reduce(lambda a, b: a[1] + b[1], expenses)) # 200

reduce() is not available by default like map() and filter(). You need to import it from the standard library module functools.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

Python provides 3 useful global functions we can use to work with collections: map(), filter() and reduce().

Tip: sometimes list comprehensions make more sense and are generally considered more pythonic

reduce() is used to calculate a value out of a sequence, like a list.

For example suppose you have a list of expenses, stored as tuples, and you want to calculate the sum of a property property in each tuple, in this case the cost of each expense:

expenses = [
    ('Dinner', 80),
    ('Car repair', 120)
]

You could iterate with a loop over them:

sum = 0
for expense in expenses:
    sum += expense[1]

print(sum) # 200

Or, you can use reduce() to reduce the list to a single value:

from functools import reduce

print(reduce(lambda a, b: a[1] + b[1], expenses)) # 200

reduce() is not available by default like map() and filter(). You need to import it from the standard library module functools.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-02-28T05:00:00+00:00) How to use Python reduce(). Retrieved from https://www.scien.cx/2021/02/28/how-to-use-python-reduce/

MLA
" » How to use Python reduce()." flaviocopes.com | Sciencx - Sunday February 28, 2021, https://www.scien.cx/2021/02/28/how-to-use-python-reduce/
HARVARD
flaviocopes.com | Sciencx Sunday February 28, 2021 » How to use Python reduce()., viewed ,<https://www.scien.cx/2021/02/28/how-to-use-python-reduce/>
VANCOUVER
flaviocopes.com | Sciencx - » How to use Python reduce(). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/28/how-to-use-python-reduce/
CHICAGO
" » How to use Python reduce()." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/02/28/how-to-use-python-reduce/
IEEE
" » How to use Python reduce()." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/02/28/how-to-use-python-reduce/. [Accessed: ]
rf:citation
» How to use Python reduce() | flaviocopes.com | Sciencx | https://www.scien.cx/2021/02/28/how-to-use-python-reduce/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.