TechieClues TechieClues
Updated date Feb 22, 2021
This code snippet helps you to understand how to check if a list is empty or not in Python. We can use 3 methods to find out whether the list contains any item in it or not.
  • 1.3k
  • 0
  • 0

This code snippet helps you to understand how to check if a list is empty or not in Python. We can use 3 methods to find out whether the list contains any item in it or not.

  •     Using If not Condition
  •     Using If and Compare List
  •     Using len() method

1. Using If not Condition:

>>> list=[] # Empty List
>>> if not list:
        print ("This list is empty")
    else:
        print ("This list is not empty")

 Output:

# Output
This list is empty   

2. Using If and Compare List:

>>> list=[] # Empty list
>>> if list==[]:
       print ("list is empty")
    else:
       print ("list is not empty")

Output:

# Output
This list is empty   

3. Using len() method:

>>> list=[] # Empty list
>>> if len(list)==0:
       print ("list is empty")
    else:
       print ("list is not empty")

Output:

# Output
This list is empty   

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!