len
used as condition PYL-C1802len(SEQUENCE)
without comparison to determine if a sequence is empty175 def get_filter_value(name, filters_instance=None):
176 filter_items = list(filter(lambda x: x['name'] == name, data.get('filters', [])))
177
178 if not len(filter_items):179 return
180
181 return filter_items[0]['value']
len(SEQUENCE)
without comparison to determine if a sequence is empty 914 pagination_selections = self.get_selections(info, ['pagination']) or []
915 pagination_names = list(map(lambda x: x.name.value, pagination_selections))
916
917 if len(pagination_names): 918 limit = self.get_pagination_limit(pagination)
919 offset = pagination.get('offset')
920 page = pagination.get('page')
len(SEQUENCE)
without comparison to determine if a sequence is empty 869 data_names = list(map(lambda x: x.name.value, data_selections))
870 model_attrs = dict(map(lambda x: [clean_name(x), getattr(Model, x)], dir(Model)))
871 only_columns = list(filter(lambda x: x is not None, map(lambda x: model_attrs.get(x), field_names))) \
872 if len(field_names) and 'allAttrs' not in data_names else None 873
874 queryset = self.get_queryset(request, Model, only_columns)
875
len(SEQUENCE)
without comparison to determine if a sequence is empty 594 lambda x: getattr(x['instance'], lookup_data['source_column'], None) in values,
595 lookup_data['aggregated_values']
596 ))
597 item_result['aggregated'] = model_values[0]['value'] if len(model_values) else 0 598
599 result[lookup_name] = item_result
600
len(SEQUENCE)
without comparison to determine if a sequence is empty 581 if lookup_data['return_list']:
582 item_result['value'] = values
583 else:
584 item_result['value'] = values[0] if len(values) else None 585
586 if 'related' in lookup_data:
587 item_result['related'] = self.filter_lookup_models(
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.")