Sabari M Sabari M
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.

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

Sabari M
Sabari M
Software Professional, India

IT professional with 14+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

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