Check if a list is empty or not in Python

In this Python tutorial, we will discuss if a list is empty or not empty in Python.

Check if the list is empty or not

Here we first need to understand the list in Python.

  • Lists are used to store the different items in a single variable. A list is a form of mutable data type i.e, which will we add or remove the data as per need. A list is one of the types of built-in data types to store the different variables i.e, Integer, Float, String.
                 sample_list = [1, 'Georgia', 'Antarctica', 54.643 ]

The list is a collection that is ordered and changeable. It also allows the duplication of data to be possible in the list.

Check if the list is empty or not:-

The empty list will consider false in Python if the return bool will be wrong i.e, False because the list becomes empty.

Different methods to check whether the list becomes empty or not in Python :

Check the list with not operator.

 
    # Check whether the list become empty or not in Python

new_list = [ 12,56.54,'Hello','Owen']    

if not new_list:
    print("List is Empty")
else:
    print("List is not Empty")

Code_Output_1:-

List is not Empty

 

Check the list with the len() method.

# Check whether the list become empty or not in Python

new_list1=[12,56.54,'Hello','Owen']    

if len(new_list1)==0:
    print("List is Empty")
else:
    print("List is not Empty",new_list1)

Code_Output_2 :

List is not Empty, [12,56.54,'Hello','Owen']

 

Check the list with another empty list.

             # Check whether the list become empty or not in Python

Lst=[]    # Empty List
new_list1=['Skills','being','required','in','Professional_Life']     # Filled list

if new_list1==Lst:
    print("List is Empty")
else:
    print("List is not Empty",new_list1)

Code_Output_3:-

List is not Empty ['Skills', 'being', 'required', 'in', 'Professional_Life']

Here by these 3 methods, we check whether the list becomes empty or not In Python(3.8). We will also check them by inputting the list in the python code to check that they are empty or not.

References

For further reference, use the link below to learn more about the Python List Built-in Module.

Leave a Reply

Your email address will not be published. Required fields are marked *