Last updated: Apr 8, 2024
Reading timeยท5 min
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')
.
Here is an example of how the error occurs.
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.
You can use the append()
method to add an item to a list.
my_list = ['apple', 'banana'] my_str = 'melon' my_list.append(my_str) print(my_list) # ๐๏ธ ['apple', 'banana', 'melon']
The list.append() method adds an item to the end of the list.
The method returns None as it mutates the original list.
The list.extend() method takes an iterable and extends the list by appending all of the items from the iterable.
list_1 = ['bobby'] list_2 = ['hadz', '.', 'com'] list_1.extend(list_2) print(list_1) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
The list.extend
method returns None
as it mutates the original list.
Alternatively, you can wrap the string in a list if you'd prefer to use the addition (+) operator.
my_list = ['apple', 'banana'] my_str = 'melon' result = my_list + [my_str] print(result) # ๐๏ธ ['apple', 'banana', 'melon']
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.
remove()
method if you need to remove an item from a listConversely, if you need to remove an item from a list, use the remove()
method.
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
.
If you meant to print the contents of the list, use a formatted string literal.
my_list = ['apple', 'banana'] my_str = 'are fruits' result = f'{my_list} {my_str}' print(result) # ๐๏ธ ['apple', 'banana'] are fruits
f
.Make sure to wrap expressions in curly braces - {expression}
.
If you meant to concatenate an item from the list with a string, access the item at the specific index using square brackets.
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.
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.
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.
Here is an example of how the error occurs.
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.
None
value and correct the assignment or check if the variable doesn't store None
before concatenating.None
valuesThe most common sources of a None
value are:
None
implicitly).None
.None
Here is an example of getting a None
value from a function that doesn't return
anything (implicitly returns None
).
# ๐๏ธ 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.
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
.
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.
None
Alternatively, you can assign a fallback value to the variable if it is None
.
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.
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.
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
.
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.
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 of whether the condition is met.