TypeError: can only concatenate list (not "X") to list [Fix]

avatar
Borislav Hadzhiev

Last updated: Jan 30, 2023
5 min

banner

# Table of Contents

  1. TypeError: can only concatenate list (not "str") to list
  2. TypeError: can only concatenate list (not NoneType) to list

# TypeError: can only concatenate list (not "str") to list

The Python "TypeError: can only concatenate list (not "str") to list" occurs when we try to concatenate a list and a string.

To solve the error, use the append() method to add an item to the list, e.g. my_list.append('my item').

typeerror can only concatenate list not str to list

Here is an example of how the error occurs.

main.py
my_list = ['apple', 'banana'] my_str = 'melon' # โ›”๏ธ TypeError: can only concatenate list (not "str") to list result = my_list + my_str

If you got the error TypeError: can only concatenate list (not NoneType) to list, click on the following subheading:

We tried to use the addition (+) operator to add an item to a list which caused the error.

# Use the append method to add an item to the end of a list

You can use the append() method to add an item to a list.

main.py
my_list = ['apple', 'banana'] my_str = 'melon' my_list.append(my_str) print(my_list) # ๐Ÿ‘‰๏ธ ['apple', 'banana', 'melon']

use append method to add an item to-the end of the list

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

The method returns None as it mutates the original list.

# Use the extend method to add multiple items to a list

The list.extend method takes an iterable and extends the list by appending all of the items from the iterable.

main.py
list_1 = ['bobby'] list_2 = ['hadz', '.', 'com'] list_1.extend(list_2) print(list_1) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']

use the extend method to add multiple items to a list

The list.extend method returns None as it mutates the original list.

# Wrap the string in a list and use the addition operator

Alternatively, you can wrap the string in a list if you'd prefer to use the addition (+) operator.

main.py
my_list = ['apple', 'banana'] my_str = 'melon' result = my_list + [my_str] print(result) # ๐Ÿ‘‰๏ธ ['apple', 'banana', 'melon']

wrap string in list and use the addition operator

However, using the append() and extend() methods is much more common and intuitive.

Using the addition (+) operator to add items to a list doesn't change the original, it returns a new list.

# Use the remove() method if you need to remove an item from a list

Conversely, if you need to remove an item from a list, use the remove() method.

main.py
my_list = ['apple', 'banana'] my_str = 'banana' my_list.remove(my_str) print(my_list) # ๐Ÿ‘‰๏ธ ['apple']

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

The method raises a ValueError if there is no such item.

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

# Use a formatted-string literal to print the contents of a list

If you meant to print the contents of the list, use a formatted string literal.

main.py
my_list = ['apple', 'banana'] my_str = 'are fruits' result = f'{my_list} {my_str}' print(result) # ๐Ÿ‘‰๏ธ ['apple', 'banana'] are fruits
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

# Concatenate an item from the list with a string

If you meant to concatenate an item from the list with a string, access the item at the specific index using square brackets.

main.py
my_list = ['apple', 'banana'] my_str = ' is my fav fruit' result = my_list[0] + my_str print(result) # ๐Ÿ‘‰๏ธ 'apple is my fav fruit'

We accessed the list element at index 0, which is a string, so we were able to concatenate the two strings.

If you aren't sure what type of object a variable stores, use the built-in type() class.

main.py
my_list = ['apple', 'banana'] print(type(my_list)) # ๐Ÿ‘‰๏ธ <class 'list'> print(isinstance(my_list, list)) # ๐Ÿ‘‰๏ธ True my_str = 'are fruits' print(type(my_str)) # ๐Ÿ‘‰๏ธ <class 'str'> print(isinstance(my_str, str)) # ๐Ÿ‘‰๏ธ True

The type class returns the type of an object.

The isinstance function returns True if the passed-in object is an instance or a subclass of the passed-in class.

# 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 None values

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.

# A function that doesn't return anything returns None

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.

# Use a fallback value if the variable stores None

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.

# A function that returns a value only if a condition is met

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.

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.

Copyright ยฉ 2024 Borislav Hadzhiev