Append to List if not None in Python

avatar

Borislav Hadzhiev

Last updated: Aug 17, 2022

banner

Photo from Unsplash

Append to List if not None in Python #

To append a value to a list only if not None:

  1. Check if the value is not None.
  2. If the condition is met, use the list.append() method to add the value to the list.
main.py
my_list = [] value = 'apple' # ✅ append to list only if not None if value is not None: my_list.append(value) print(my_list) # 👉️ ['apple'] # ----------------------------- # ✅ filter out all None values from list my_list = ['apple', None, 'banana', None] new_list = [i for i in my_list if i is not None] print(new_list) # 👉️ ['apple', 'banana']

We first check if the value is not None.

You should use the is operator when you need to check if a variable stores a None value.

When we use is, we check for the object's identity.

The pep 8 style guide mentions that comparison to singletons like None should always be done with is or is not, and never the equality operators.

Use the equality operators (equals == and not equals != ) when you need to check if a value is equal to another value, e.g. 'abc' == 'abc'.

If the value is not None, we use the list.append() method to add it.

main.py
my_list = [] value = 'apple' # ✅ append to list only if not None if value is not None: my_list.append(value) print(my_list) # 👉️ ['apple']

The list.append() method adds an item to the end of the list.

The method returns None as it mutates the original list.

If you have a list that contains None values and you need to filter them out, use a list comprehension.

main.py
my_list = ['apple', None, 'banana', None] new_list = [i for i in my_list if i is not None] print(new_list) # 👉️ ['apple', 'banana']
List comprehensions 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 None and return the result.

The new list doesn't contain any None values.

The list comprehension doesn't mutate the original list, it returns a new list.

If you need to remove the None values from the original list, use a for loop.

main.py
my_list = ['apple', None, 'banana', None] for item in my_list.copy(): if item is None: my_list.remove(item) print(my_list) # 👉️ ['apple', 'banana']

We used the list.copy() method to get a copy of the list.

The list.copy method returns a shallow copy of the object on which the method was called.

This is necessary because we aren't allowed to remove elements from a list while iterating over it.

However, we can iterate over a copy of the list and remove elements from the original list.

On each iteration, we check if the current item is None and use the list.remove() method to remove the matching elements.

The list.remove() method removes the first item from the list whose value is equal to the passed in argument.

The remove() method mutates the original list and returns None.

The most important thing to note when removing items from a list while iterating is to use the list.copy() method to iterate over a copy of the list.

If you try to remove elements from the original list while iterating over it, you might run into difficult to locate bugs.

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.