9951 explained code solutions for 126 technologies


pythonHow to check if list is empty


if list:
  print("list is not empty")
else:
  print("list is empty")ctrl + c
list

name of the list variable to check for emptiness

print("list is not empty")

this code will execute if list is not empty


Usage example

list1 = [1, 2, 3]
list2 = []

if list1:
  print("list1 is not empty")
else:
  print("list1 is empty")

if list2:
  print("list2 is not empty")
else:
  print("list2 is empty")
output
list1 is not empty
list2 is empty