How do I check if a list is empty?

Technology CommunityCategory: PythonHow do I check if a list is empty?
VietMX Staff asked 3 years ago

Based on PEP 8 style guide, for sequences (strings, lists, tuples), use the fact that empty sequences are false:

if not list:
  print("List is empty")

My problem with if not list: ... is that it gives the false impression that li is a boolean variable.

So more clear, explicit way would be:

if len(list) == 0:
    print('the list is empty')