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 Dictionary update()
Python Program to Find Hash of File
Python Program to Differentiate Between type() and isinstance()
Python Program to Convert Two Lists Into a Dictionary
Python String rpartition()
Python Tuple index()
Python String rindex()
Python Program to Find the Size (Resolution) of a Image
Python frozenset()
Python Machine Learning - Sebastian Raschka
Python Program to Find Numbers Divisible by Another Number
Python Generators
Python Set discard()
Python Program to Count the Occurrence of an Item in a List
Python Program to Get the Class Name of an Instance
Python List pop()
Python String title()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Program to Solve Quadratic Equation
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python str()
Python Program to Convert Decimal to Binary Using Recursion
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Dictionary items()
Python Set add()
Python Program to Check Armstrong Number
How to Find an Element in a List with Java
Python print()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python String rjust()
Python memoryview()
Python Program to Find the Largest Among Three Numbers