Get the first not None value in a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 24, 2022

banner

Photo from Unsplash

Get the first not None value in a List in Python #

Use the next() function to get the first not None value in a list, e.g. first = next(item for item in my_list if item is not None). The next() function will return the first not None value in the list.

main.py
# ✅ Get the first not None value in a list my_list = [None, None, None, 'a', None, 'b'] first = next(item for item in my_list if item is not None) print(first) # 👉️ 'a' # ------------------------------------- # ✅ Handle the edge case where the list contains only None values my_list = [None, None] first = next((item for item in my_list if item is not None), 'default if all are None') print(first) # 👉️ 'default if all are None'

We called the next() function with a generator expression to get the first not None value in a list.

The next function returns the next item from the iterator.

main.py
my_list = [None, None, None, 'a', None, 'b'] first = next(item for item in my_list if item is not None) print(first) # 👉️ 'a'

We used a generator expression to iterate over the list.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current item is not None and return the result.

The next() function returns the first matching value.

If you need to handle the scenario where the list contains only None values, pass a default value argument to the next() function.

main.py
my_list = [None, None] first = next((item for item in my_list if item is not None), 'default if all are None') print(first) # 👉️ 'default if all are None'
The second argument we passed to the next() function is a default value that gets returned if the condition is never met.

Make sure to wrap the generator expression in parentheses.

If no default value is provided and the iterator is exhausted, the next() function raises a StopIteration exception.

Alternatively, you can use a for loop.

Get the first not None value in a List using for loop #

To get the first not None value in a list:

  1. Use a for loop to iterate over the list.
  2. On each iteration, check if the current value is not None.
  3. If the condition is met, assign the value to a variable and break out of the loop.
main.py
my_list = [None, None, None, 'a', None, 'b'] first = '' for item in my_list: if item is not None: first = item break print(first) # 👉️ 'a'

We initialized the first variable to an empty string, but you can use any other value that suits your use case, e.g. 0.

On each iteration of the for loop, we check if the current value is not None.

If the condition is met, we assign the value to the first variable and break out of the loop to avoid iterating needlessly.

The break statement breaks out of the innermost enclosing for or while loop.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.