Looking at one's own old, bad code can be a source of great cringe.
I remember prepending if incidents is not None because if incidents is None, then a TypeError is raised on the second condition. This line can be reduced to if incidents: without any change in behavior.
Checking the list length before iterating over it is a no-op.
Example
# Bad
incidents = get_ticket_ids(cell.value)
if incidents is not None and len(incidents) > 0:
for incident in incidents:
print(incident)
# Good
incidents = get_ticket_ids(cell.value)
if incidents:
for incident in incidents:
print(incident)
# Good
if incidents := get_ticket_ids(cell.value):
for incident in incidents:
print(incident)
Explanation
Looking at one's own old, bad code can be a source of great cringe.
I remember prepending
if incidents is not None
because if incidents is None, then aTypeError
is raised on the second condition. This line can be reduced toif incidents:
without any change in behavior.Checking the list length before iterating over it is a no-op.
Example