len
used as condition PYL-C1802len(SEQUENCE)
to determine if a sequence is empty
80 info = row[3:]
81 except BaseException:
82 info = None
83 if len(info) == 0:
84 tableData.append([title, city, ''])
85 tableData.append([name, "", ''])
86 else:
len(SEQUENCE)
to determine if a sequence is empty
26 result = requests.get(get_event_url)
27 events_info = result.json()
28 events_list = []
29 if len(events_info) == 0:
30 return None
31 else:
32 for event in events_info:
len(SEQUENCE)
to determine if a sequence is empty
43 "link": event["url"]})
44 else:
45 break
46 if len(events_list) == 0:
47 return None
48 else:
49 if len(events_list) > 3:
len(SEQUENCE)
to determine if a sequence is empty
13 result = requests.get(get_event_url)
14 events_info = result.json()["entry"]
15 events_list = []
16 if len(events_info) == 0:
17 return None
18 else:
19 for event in events_info:
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.")