Do not use
len(SEQUENCE)
without comparison to determine if a sequence is empty459 # Clean up
460 while True:
461 users = client.get("/users/1").json()
462 if not len(users):463 break
464 for user in users:
465 client.delete(f'/user/{user["username"]}')
Description
Using the len
function to check if a sequence is empty is not idiomatic and can be less performant than checking the truthiness of the object.
len
doesn't know the context in which it is called, so if computing the length means traversing the entire sequence, it must; it doesn't know that the result is just being compared to 0.
Computing the boolean value can stop after it sees the first element, regardless of how long the sequence actually is.
Not preferred:
if not len(my_sequence):
print("Empty sequence.")
else:
print("Sequence is not empty.")
Preferred:
if not my_sequence:
print("Empty sequence.")
else:
print("Sequence is not empty.")