TypeError: can only concatenate list (not NoneType) to list

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: can only concatenate list (not NoneType) to list #

The Python "TypeError: can only concatenate list (not "NoneType") to list" occurs when we try to concatenate a list and a None value. To solve the error, correct the assignment or check if the variable doesn't store a None value before concatenating.

typeerror can only concatenate list not nonetype to list

Here is an example of how the error occurs.

main.py
my_list = ['a', 'b'] example = None # ⛔️ TypeError: can only concatenate list (not "NoneType") to list result = my_list + example

We tried to use the addition (+) operator to concatenate a list and a None value which caused the error.

To solve the error, we either have to figure out where the variable got assigned a None value and correct the assignment or check if the variable doesn't store None before concatenating.

The most common sources of a None value are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

Here is an example of getting a None value from a function that doesn't return anything (implicitly returns None).

main.py
# 👇️ this function returns None def get_list(): print(['c', 'd']) my_list = ['a', 'b'] # ⛔️ TypeError: can only concatenate list (not "NoneType") to list result = my_list + get_list()

The get_list function doesn't return anything, so it implicitly returns None.

You can use a return statement to return a value from the function.

main.py
def get_list(): return ['c', 'd'] my_list = ['a', 'b'] result = my_list + get_list() print(result) # 👉️ ['a', 'b', 'c', 'd']

You can use an if statement if you only want to concatenate the values if the variable doesn't store None.

main.py
my_list = ['a', 'b'] my_other_list = None if my_other_list is not None: result = my_list + my_other_list print(result) else: # 👇️ this runs print('variable stores a None value')

The if block is only run if the my_other_list variable doesn't store a None value, otherwise the else block is run.

Alternatively, you can assign a fallback value to the variable if it is None.

main.py
my_list = ['a', 'b'] my_other_list = None if my_other_list is None: my_other_list = [] result = my_list + my_other_list print(result) # 👉️ ['a', 'b']

We check if the my_other_list variable stores a None value, and if it does, we set it to an empty list.

Note that many built-in methods mutate the original object (e.g.sort()) and therefore don't return anything (implicitly return None), so make sure you aren't storing the result of calling one in a variable.

Another common source of None values is having a function that only returns a value if a certain condition is met.

main.py
def get_list(a): if len(a) > 2: return a result = get_list(['a', 'b']) print(result) # 👉️ None

The if statement in the get_list function is only run if the passed in argument has a length greater than 2.

In all other cases, the function doesn't return anything and ends up implicitly returning None.

To solve the error in this scenario, you either have to check if the function didn't return None, or return a default value if the condition is not met.

main.py
def get_list(a): if len(a) > 2: return a return [] result = get_list(['a', 'b']) print(result) # 👉️ []

Now the function is guaranteed to return a value regardless if the condition is met.

Conclusion #

The Python "TypeError: can only concatenate list (not "NoneType") to list" occurs when we try to concatenate a list and a None value. To solve the error, correct the assignment or check if the variable doesn't store a None value before concatenating.

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.