. Advertisement .
..3..
. Advertisement .
..4..
“TypeError: can only concatenate list (not “int”) to list” is a common error that shows up in many ways. What is the cause of it and how to fix it? Let’s read this article, we will give you some helpful solutions to solve this error.
How Does The Error “TypeError: can only concatenate list (not “int”) to list” Happen?
When you run your program, you easily get the error “TypeError: can only concatenate list (not “int”) to list” as shown below:
exp_list = [1878, 1902, 1899, 1888, 1893]
exp_integer = 1892
result1 = exp_list + exp_integer
print(result1)
Error message:
Traceback (most recent call last):
File "code.py", line 3, in <module>
result1 = exp_list + exp_integer
TypeError: can only concatenate list (not "int") to list
This error happens when you attempt to concatenate a list and an integer. In the above example, you added an item to a list by using the addition (+
) operator, and it caused the error.
How Can We Solve The Error “TypeError: can only concatenate list (not “int”) to list”?
Method 1: Utilize the append()
The simplest way for you to solve the error “TypeError: can only concatenate list (not “int”) to list” is to utilize the append() as the following example:
exp_list = [1878, 1902, 1899, 1888, 1893]
exp_integer = 1892
exp_list.append(exp_integer)
print(exp_list)
Output:
[1878, 1902, 1899, 1888, 1893, 1892]
This method adds a single element to a list containing items. Instead of being returned to a new list, the element will be added to the end of the existing one. Adds its argument to the end of a list like a single element. The list gets one more item on it. The method modifies the initial list, so it returns None.
Method 2: Add the integer number to every element of the list
You also add the integer number to every element of the list as the following to fix the error “TypeError: can only concatenate list (not “int”) to list”.
list_number = [28, 22, 29, 19, 13]
sample_number = 12
for j in range(len(list_number)):
list_number[j] += sample_number
print(list_number)
Output:
[40, 34, 41, 31, 25]
Method 3: Utilize the remove()
Let’s utilize the remove()
method when you want to delete an item from a list as the example below:
list_number = [1878, 1892, 1888, 1902, 1901]
remove_number = 1888
list_number.remove(remove_number)
print(list_number)
Output:
[1878, 1892, 1902, 1901]
The first item from the list whose value matches the argument supplied gets removed by the list.remove() method. If there is no such item, the method raises a ValueError. The original list is altered using the remove()
method, and the remove()
method returns None.
Method 4: Utilize square brackets
Use square brackets to retrieve the item at its index if you want to add a specific item from the list to an integer.
list_number = [1878, 1892, 1888, 1902, 1901]
exp_integer = 18
result = list_number[3] + 3
print(result)
Output:
1905
We added the integer kept in the exp_integer variable to the list item accessed at index 3. Use the type() class built-in if you are unsure about the kind that variable stores.
exp_integer = 17
print(type(exp_integer))
print(isinstance(exp_integer, int))
list_number = [1878, 1892, 1888, 1902, 1901]
print(type(list_number))
print(isinstance(list_number, list))
Output:
<class 'int'>
True
<class 'list'>
True
The type class returns the type of an object. If the object passed in is an example of a subclass of the given class, the isinstance function will return True.
Conclusion
If the you are having difficulties with the problem “TypeError: can only concatenate list (not “int”) to list”, let’s try the above remedies. We believe with these solutions, you can easily resolve your problem. If you need assistance or have other questions, many researchers are always keen to help. Finally, we hope all of our readers have a splendid day filled with new ideas. Thank you for your reading!
Read more
→ Typeerror: can only concatenate str (not “int”) to str – How to fix?
Leave a comment