Check if a list is empty in Python
October 25, 2020 ‐ 1 min read
Testing whether a list is empty is quite straightforward in Python. In the context of a
while statement an empty list will be evaluated as
False. Like how empty strings or empty dicts are evaluated as
False as well.
glass =  if not glass: print('Glass is empty') # => Glass is empty
The PEP8 style guide prefers the option as shown above. But in case you rather do a more explicit check the following will do. By comparing with
 we know for sure that
glass is a list.
glass =  if glass == : print('Glass is empty') # => Glass is empty
Another option is using the
len() function in case you rather be somewhat explicit. This way we validate that
glass is at least a sequence(str, list, tuple, ...).
glass =  if len(glass) == 0: print('Glass is empty') # => Glass is empty