How to Check If a List is Empty or Not in Python?
Hi Artisan,
Now, let's see post of python check list is empty or not. This article details how to check whether the list is empty or not in python. I want to share with you how to check if python list is not empty. you can understand the concept of how to check if an element in a list is empty python. Let's get started with python check if list is empty or not.
There are many ways to check if list is empty or not in python. i will give you some examples using If Condition, If Not Condition, bool() Function and len() Function to check whether list is empty or not in python. so let's see the below examples.
Without any further ado, let's see the code examples below one by one.
You can use these examples with python3 (Python 3) version.
Example 1: using If Condition
main.py
myList = [] # Check List Empty or not if myList: print("List is not empty.") else: print("List is empty.")
Output:
List is empty.
Example 2: using If Not Condition
main.py
myList = [] # Check List Empty or not if not myList: print("List is empty.") else: print("List is not empty.")
Output:
List is empty.
Example 3: using bool() Function
main.py
myList = [] # Check List Empty or not if bool(myList): print("List is empty.") else: print("List is not empty.")
Output:
List is empty.
Example 4: using len() Function
main.py
myList = [] # Check List Empty or not if len(myList): print("List is not empty.") else: print("List is empty.")
Output:
List is empty.
Example 5: using len() with "0" Function
main.py
myList = [] # Check List Empty or not if len(myList) == 0: print("List is empty.") else: print("List is not empty.")
Output:
List is empty.
I hope it can help you...