Separate an empty list from a missing value
Type one expression at a time and check its delimiters. · 183 words
An experiment can have no items or no value, and Python treats them differently. An empty list, [], is a real list with zero items. None is a special value meaning that a value is missing or was not supplied. For example, features might be [] when a museum image has no detected objects, but None when the detection program failed. Test the missing value first with if features is None:, because None is not a list. Then use if features == []: to recognize an empty list specifically.
A clean experiment function should state which state it returns. If scan_image returns [] for a valid image with no objects and None for a failed scan, the caller can react differently: report "no objects detected" for [], but "scan unavailable" for None. Do not write features = features or [], because that silently changes a missing result into a valid empty list. The same distinction appears in labels: [] can mean no training examples were collected, while None can mean collection never happened. Separate states prevent missing data from looking like measured zeros.
Check your recall
In an experiment, result can be [] or None. What does each mean, and which check detects a missing value?
Check your answer
[] is an existing list that contains no items. None means that no value was supplied or produced. Use if result is None: to detect the missing value.
Sources and credits
Original KeyTouch lesson. Background reading · Python Software Foundation · The Python Tutorial.