Check List is Empty or Not in Python Example

23-Dec-2022

.

Admin

Check List is Empty or Not in Python Example

Hi Dev,

Today, I will let you know example of Check List is Empty or Not in Python. I explained simply step by step python program to check whether list is empty or not. Here you will learn python check if list is empty or not. you will learn how to check if an element in a list is empty python.

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.

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 = [myList = []

# Check List Empty or not

if len(myList) == 0:

print("List is empty.")

else:

print("List is not empty.")

Output:

List is empty.

#Python