Review Note

Last Update: 07/10/2024 01:20 PM

Current Deck: Python fundamentals

Published

Fields:

Original
from functools import reduce

numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product)

Better
from functools import reduce
from operator import mul

numbers = [1, 2, 3, 4, 5]
product = reduce(mul, numbers)
print(product)

Comments
The operator module provides operators as functions.

Prefer them to lambdas.
Context
Python operator

Tags:

python_fundamentals python_operator

Suggested Changes:

Deck Changes (Suggestion to move the Note to the following Deck):

Field Changes:

Tag Changes: