Lists are used pretty much all over the place in Python and in other programming languages as well.
Checking whether they have elements or not represents something that you may need to do.
One interesting thing about Python is that it allows you to do the same thing in more than just one way, as we can see in this case.
First method
Let us first see the first method that we can use to check whether a list is empty in Python or not.
It has to do with checking whether the length of the list is 0 or not. In case the length is 0, yes, you are right, it is empty:
my_list = []
if len(my_list) == 0:
print("This list is empty")
It’s that simple, yes.
Second method
The second method has to do with checking whether a list has any elements or not”
my_list = []
if not my_list:
print("This list is empty")
Third method
In the third method, we need to can basically compare a list with an empty array like below:
my_list = []
if my_list == []:
print("This list is empty")
That’s pretty much it.
You can use whichever you want in your day-to-day programming journey.