Check if multiple variables are not None in Python

avatar

Borislav Hadzhiev

Last updated: Jul 15, 2022

banner

Check out my new book

Check if multiple variables are not None in Python #

To check if multiple variables are not None:

  1. Wrap the variables in a list.
  2. Use a generator expression to iterate over the list.
  3. On each iteration, check if the current list item is not None.
  4. Pass the generator object to the all() function.
main.py
a = 'a' b = 'b' c = 'c' if all(item is not None for item in [a, b, c]): print('Multiple variables are NOT None') else: print('At least one of the variables stores a None value')

We used square brackets to add the variables to a list and 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 list item is not None and return the result.

The last step is to pass the generator object to the all() function.

The all() built-in function takes an iterable as an argument and returns True if all elements of the iterable are truthy (or the iterable is empty).

If all of the variables are not None, the all() function will return True, otherwise False is returned.

An alternative approach is to use the in operator.

To check if multiple variables are not None:

  1. Wrap the variables in a sequence (e.g. a tuple or a list).
  2. Use the not in operator to check if None is not a member of the sequence.
  3. If the sequence doesn't contain None, then the variables are not None.
main.py
a = 'a' b = 'b' c = 'c' if None not in (a, b, c): # 👇️ this runs print('Multiple variables are NOT None')

This approach looks much simpler than the previous one. However, the in and not in operators check for equality, e.g. None != a, None != b, etc.

This is not a good practice in Python, as it is recommended to check for None using the is keyword.

There are some very rare cases where using equals and not equals to check forNone might lead to confusing behavior, so the PEP8 style guide recommends using is and is not when testing for None.

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise it evaluates to False.

x not in l returns the negation of x in l.

An alternative approach is to use the and operator multiple times.

main.py
a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print('Multiple variables are NOT None')

This is generally not recommended, as it is quite repetitive and hard to read.

The if statement first checks if the a variable is not None. If it it, the if statement short-circuits returning False without checking any of the following conditions.

The expression x and y returns the value to the left if it's falsy, otherwise the value to the right is returned.

main.py
result = False and 'hello' print(result) # 👉️ False

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 0 (zero) of any numeric type
  • empty sequences and collections: "" (empty string), () (empty tuple), [] (empty list), {} (empty dictionary), set() (empty set), range(0) (empty range).

So if the value to the left is any of the aforementioned falsy values, the value to the left is returned.

It's best to use the all() function to check if multiple variables are not None, as this approach doesn't have any edge cases and is quite readable.
main.py
a = 'a' b = 'b' c = 'c' if all(item is not None for item in [a, b, c]): print('Multiple variables are NOT None') else: print('At least one of the variables stores a None value')
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.