Table of Contents
In this example, you will learn to check if a Python list is empty.
To understand this example, you should have the knowledge of the following Python programming topics:
1. Example 1: Using Boolean operation
my_list = []
if not my_list:
print("the list is empty")
Output
the list is empty
If my_list is empty then not returns True.
It is the most pythonic way of testing emptiness. If you want to learn more about boolean truth value, you can refer to Truth Value Testing.
2. Example 2: Using len()
my_list = []
if not len(my_list):
print("the list is empty")
Output
the list is empty
In this example, length of list is used to check if there is any element in the list. If the length of a list is 0, then the list is empty.
3. Example 3: Comparing with []
my_list = []
if my_list == []:
print("The list is empty")
Output
[] is an empty list, therefore if my_list has no elements, then it should be equal to [].
Related posts:
Python List copy()
Python List
Python Machine Learning Eqution Reference - Sebastian Raschka
Python any()
Python String rpartition()
Python Program to Transpose a Matrix
Python Program to Make a Flattened List from Nested List
Python String isalpha()
Python Tuple count()
Converting between an Array and a List in Java
Python Variables, Constants and Literals
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Differentiate Between del, remove, and pop on a List
Python set()
Python Program to Convert Two Lists Into a Dictionary
Python Package
Python String isprintable()
How to Find an Element in a List with Java
Python Program to Represent enum
Python String join()
Python List index()
Python filter()
Python Type Conversion and Type Casting
Python int()
Python id()
Python Program to Find ASCII Value of Character
Remove the First Element from a List
Python Program to Print Output Without a Newline
Python Program to Display Calendar
Removing all Nulls from a List in Java
Python Program to Parse a String to a Float or Int
Python strftime()