Check if all elements in a list are None in Python

In this article we will discuss different ways to check if a list contains only None or not.

Suppose we have a list,

sample_list = [None, None, None, None]

Now we want to confirm that all items in this list are None. There are different ways to do that. Let’s discuss them one by one,

Check if a list contains only None using for loop

The first solution that comes to our mind is using for loop.
Logic is, Iterate over all the items of a list using for loop and for each item check if it is None or not. As soon as a non None item is found, break the loop because it means all items of list are not None. Whereas, if the loop ends and not even a single non None item is found, then it proves that all items of the list are None.

Let’s create a function that accepts a list and check if all items are none or not using the above logic,

def check_if_all_none(list_of_elem):
    """ Check if all elements in list are None """
    result = True
    for elem in list_of_elem:
        if elem is not None:
            return False
    return result

Use this function to check if all items in list are None,

sample_list = [None, None, None, None]

# Check if list contains only None
result = check_if_all_none(sample_list)

if result:
    print('Yes List contains all None')
else:
    print('Not all items in list are None')

Output:

Yes List contains all None

Generic function to check if all items in list matches a given item like None

In the above example, we created a separate function just to check if all items in a list are None or not, although it does the work but it is not a reusable function. So, let’s create a generic function to check if all items of a list are same and also matches the given element,

def check_if_all_same(list_of_elem, item):
    """ Check if all elements in list are same and matches
     the given item"""
    result = True
    for elem in list_of_elem:
        if elem != item:
            return False
    return result

This function accepts a list and an item as an argument. Then checks if all items in the given list matches the given item using the same logic as previous example. We can use this function to check if all items of list are None,

sample_list = [None, None, None, None]

# Check if all items in list are same and are None
result = check_if_all_same(sample_list, None)

if result:
    print('Yes List contains all None')
else:
    print('Not all items in list are None')

Output:

Yes List contains all None

Now we can use this function at other places too like, to confirm if all elements in a list matches the given number or string etc.

Check if a list contains only None using List comprehension

Let’s look at a pythonic way to confirm if all items in a list are None,

def check_if_all_same_2(list_of_elem, item):
    """ Using List comprehension, check if all elements in list
     are same and matches the given item"""
    return all([elem == item for elem in list_of_elem])

Let’s use this function to check all items of list are None,

sample_list = [None, None, None, None]

# Check if all items in list are same and are None
result = check_if_all_same_2(sample_list, None)

if result:
    print('Yes List contains all None')
else:
    print('Not all items in list are None')

Output:

Yes List contains all None

It is a one line solution.
Using list comprehension we created a Boolean list from the existing list. Each element in this bool list corresponds to an item for the main list. Basically, in the List Comprehension we iterated over the given list and for each element we checked if it is None or not. If it is None then added True in the bool list else added False. Now using all() checked if the bool list contains only True or not. If all() function returns True, then it means our main list contains only None.

We can use this function in a generic way too. For example check if all items in the list matches a given string,

sample_list = ['is', 'is', 'is', 'is', 'is']

# Check if all items in list are string 'is'
result = check_if_all_same_2(sample_list, 'is')

if result:
    print('Yes List contains same elements')
else:
    print('Not all items in list are same')

Output:

Yes List contains same elements

The complete example is as follows,

def check_if_all_none(list_of_elem):
    """ Check if all elements in list are None """
    result = True
    for elem in list_of_elem:
        if elem is not None:
            return False
    return result


def check_if_all_same(list_of_elem, item):
    """ Check if all elements in list are same and matches
     the given item"""
    result = True
    for elem in list_of_elem:
        if elem != item:
            return False
    return result


def check_if_all_same_2(list_of_elem, item):
    """ Using List comprehension, check if all elements in list
     are same and matches the given item"""
    return all([elem == item for elem in list_of_elem])


def main():



    print('*** Check if a list contains only None using for loop ***')

    sample_list = [None, None, None, None]

    # Check if list contains only None
    result = check_if_all_none(sample_list)

    if result:
        print('Yes List contains all None')
    else:
        print('Not all items in list are None')

    print('*** Check if a list contains only None using Generic function ***')

    sample_list = [None, None, None, None]

    # Check if all items in list are same and are None
    result = check_if_all_same(sample_list, None)

    if result:
        print('Yes List contains all None')
    else:
        print('Not all items in list are None')

    print('*** Check if a list contains only None using List comprehension ***')

    sample_list = [None, None, None, None]

    # Check if all items in list are same and are None
    result = check_if_all_same_2(sample_list, None)

    if result:
        print('Yes List contains all None')
    else:
        print('Not all items in list are None')

    print('*** check if all items in the list matches a given string ***')

    sample_list = ['is', 'is', 'is', 'is', 'is']

    # Check if all items in list are string 'is'
    result = check_if_all_same_2(sample_list, 'is')

    if result:
        print('Yes List contains same elements')
    else:
        print('Not all items in list are same')


if __name__ == '__main__':
   main()

Output:

*** Check if a list contains only None using for loop ***
Yes List contains all None
*** Check if a list contains only None using Generic function ***
Yes List contains all None
*** Check if a list contains only None using List comprehension ***
Yes List contains all None
*** check if all items in the list matches a given string ***
Yes List contains same elements

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top