consider-using-any-or-all / C0501#
Message emitted:
for loop could be %s
Description:
A for loop that checks for a condition and return a bool can be replaced with any or all.
Problematic code:
def any_even(items):
"""Return True if the list contains any even numbers"""
for item in items: # [consider-using-any-or-all]
if item % 2 == 0:
return True
return False
def all_even(items):
"""Return True if the list contains all even numbers"""
for item in items: # [consider-using-any-or-all]
if not item % 2 == 0:
return False
return True
Correct code:
def any_even(items):
"""Return True if the list contains any even numbers"""
return any(item % 2 == 0 for item in items)
def all_even(items):
"""Return True if the list contains all even numbers"""
return all(item % 2 == 0 for item in items)
Note
This message is emitted by the optional 'consider-using-any-or-all' checker which requires the pylint.extensions.for_any_all
plugin to be loaded.
Created by the consider-using-any-or-all checker.