Use Next
Documentation Index
Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
Use this file to discover all available pages before exploring further.
Sourcery refactoring id: use-next
Section titled “Sourcery refactoring id: use-next”Description
Section titled “Description”Use the built-in function
next instead of a
for-loop.
Before
Section titled “Before”def get_first_even_number(numbers): for number in numbers: if number % 2 == 0: return number return Nonedef get_first_even_number(numbers): return next((number for number in numbers if number % 2 == 0), None)Explanation
Section titled “Explanation”When choosing the first item from an iterable that passes a condition, we can
use the next built-in function instead of a for-loop to make our code and our
intent clearer.