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
Comments (0)