Built with Tutify.io

    AIStudyLabs

    Check if the List is Empty in Python

    Check if the List is Empty in Python

    How to check if a list is empty in Python? In python empty list is considered False hence when you pass an empty list to the bool() function it returns False, you can use this with an if condition to check if the list is empty. Besides this, there are other ways as well to check for list emptiness which will be discussed in this article.

    Methods to check if List is Empty in Python:

    <a id="h-1-quick-examples-of-checking-list-is-empty"></a>

    1. Quick Examples of Checking List is Empty

    Following are quick examples of how to check if a list is empty.

    
    # Quick Examples of checking list is empty
    
    # Consider two list
    list1 =[1,2,3,4]
    list2 = []
    
    # Example 1: Check if list is empty
    print("Is List 1 Empty: ",not bool(list1))
    print("Is List 2 Empty: ",not bool(list2))
    
    # Example 2: Check list2 is empty using PEP8 stype
    if list2:
        print("List 1 Not Empty")
    else:
        print("List 1 Empty")
    
    # Example 3: Using not with PEP 8
    if not list2:
        print("List 2 Empty")
    
    # Example 4: Using bool()
    if bool(list2):
        print("List 2 Not Empty")
    else:
        print("List 2 Empty")
    
    # Example 5: Using not with bool()
    if not bool(list2):
        print("List 2 Empty")
    
    # Example 6: Using len()
    if len(list2):
        print("List Not Empty")
    else:
        print("List Empty")
    
    # Example 7: Using len()
    if len(list2) == 0:
        print("List Empty")
    

    Here, a list named list2 creates an empty list.

    What is considered Flase in Python?

    There are several objects in Python that are considered Flase, here are some examples.

    2. Using PEP 8 Approach

    You can do this by using PEP 8 style. The empty list is considered False in Python hence you can use it on if condition to check if the list is empty or not. If the list is empty the condition evaluates to False and the else block is executed.

    
    # Check if lis is empty
    # list1 is not empty
    if list1:
        print("List 1 Not Empty")
    else:
        print("List 1 Empty")
    
    # list2 is empty
    if list2:
        print("List 2 Not Empty")
    else:
        print("List 2 Empty")
    

    This yields the below output.

    The above example has a code block for both True and False scenarios. Sometimes you will be required to execute a block of code only when the list is empty and do nothing when the list is not empty. You can use the not operator on the if condition to invert the expression. Below is an example.

    
    # Empty List
    if not list2:
        print("List 2 Empty")
    
    # Output:
    # List 2 Empty
    

    3. Using bool() to Check List is Empty in Python

    In Python, the empty list is considered as False hence to check if the list is empty, pass the list as an argument to the bool(). if the list doesn't have any elements then it returns False otherwise True. Here is an example.

    
    # Non empty list
    list1 =[1,2,3,4]
    print("List 1: ",list1)
    
    # Empty list
    list2 = []
    print("List 2:", list2)
    
    # Check if list is empty
    print("Is List 1 Empty: ",not bool(list1))
    print("Is List 2 Empty: ",not bool(list2))
    

    This example yields the below output. Note that I used the not operator to invert the bool() result for more readability.

    Here, I have considered the list1 with some elements and list2 with no elements. since I used the not along with bool() it reverses the result hence, the result shows True for list1 and False for list2.

    4. Using bool() with if Statement

    Let's use the same bool() function and check if the list is empty or not by using the if statement.

    
    # Empty List
    if bool(list2):
        print("List 2 Not Empty")
    else:
        print("List 2 Empty")
    

    Similarly, you can also use the not bool() with if statement if you think your code is more readable.

    
    # Empty List
    if not bool(list2):
        print("List 2 Empty")
    else:
        print("List 2 Not Empty")
    

    5. Using len()

    We can also use the len() function to check if a list is empty, passing a sequence object as an argument to this function returns the number of elements in the object, for an empty list, it returns 0 which is False in Python. Here is an example

    
    # Using len()
    list2 = []
    if len(list2):
        print("List Not Empty")
    else:
        print("List Empty")
    
    # Output:
    # List Empty
    

    Here, the len(list2) is 0 hence it is considered False, and the if condition becomes false, so the else block is executed.

    Alternatively, you can also check the result of len(list2) is equal to 0 to check if the list is empty. This is similar to the above example.

    
    # Using len()
    list2 = []
    if len(list2) == 0:
        print("List Empty")
    else:
        print("List Not Empty")
    
    # Output:
    # List Empty
    

    Note that checking if the length of a list is equal to zero, rather than just checking if the list is false, is not a pythonic way of doing things hence it is not recommended approach if you wanted to follow Python idioms.

    However, if you are using arrays then you have to use other options as none of the options worked with a list doesn't work with NumPy array.

    6 Using NumPy Array to Check if it is Empty

    All the above methods explained to check if the list is empty in Python don't work with NumPy hence we have to use np.size to get the size of an array, if it is empty it returns 0 which can be used to check the array is empty or not.

    
    # Import NumPy
    import numpy as np
    
    # Create array
    x = np.array([0,1])
    
    # Check Numpy empty
    if x.size: 
        print("x")
    

    Conclusion

    In this article, you have learned different ways to check if a list is empty in Python. Once simplest and recommended approach would be using the list directly on the if condition. If the list is empty it evaluates the Flase otherwise True. You can add not to it to invert the result.

    <a id="h-related-articles"></a>

    Related Articles