Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Python: Check if list is empty

📅 2009-Sep-30 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ empty, list, python ⬩ 📚 Archive

aList = []

if not aList: # Check if list is empty
    print( "Empty" )
else:
    print( "Not empty" )

alist.append( 42 )

if alist: # Check if list is not empty
    print( "Not empty" )
else:
    print( "Empty" )

# Output:
# Empty
# Not empty

© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧