TypeError: unsupported operand type(s) for +: int and list

Python provides multiple operators to perform specific calculations on the variables and values in the program. All the operators in Python follow specific operand types and will give an error if the operand is unsupported. For instance, the addition operator can not be placed between operands like “int” and “list”. To resolve such types of errors, various methods are provided by Python.

This write-up will provide a comprehensive guide on resolving the “unsupported operand type(s) for +:int and list” error using various solutions. The contents to be discussed in this article are listed below:

So, let’s get started with the first reason.

Reason 1: Using Addition Operator With Number and List

The “unsupported operand type(s) for +: int and list” error appears in Python when a user tries to add an integer to the list using the addition operator “+”. Here is an example:

The above output shows the “TypeError” because the integer “45” is added to the list “list_value” using the addition operator “+”.

Solution 1: Use Simple for Loop

The error will be fixed using a simple “for” loop that will iterate over the list element and add the specified integer “6” into each list element. The example code is given below:

Code:

list_value = [44, 23, 45]
empty_list = []
for item in list_value:
    empty_list.append(item + 6)
print(empty_list)

In the above code:

  • The list named “list_value” and the empty list named “empty_list” is created in the program.
  • The “for” loop iterates over the list items, and on each iteration, the integer value “6” has been added to the list element using the “append()” function.

Output:

The above output verified that an integer value had been added to each list element.

Solution 2: Use List Comprehension

The other solution to resolve this error in Python is using “List Comprehension” along with the “for” loop. The List comprehension method has similar functionality to the above “for” loop method. As a single-line statement code, this method can compact the program.

Code:

list_value = [44, 23, 45]
list_a = [i + 6 for i in list_value]
print(list_a)

In the above code:

  • The list comprehension has a “for” loop that iterates over the list and adds each element of the list using the “+” operator.
  • The final output list “list_a” has been displayed on the screen utilizing the “print()” function.

Output:

This output shows that each element has been added to an integer “6”.

Solution 3: Access Specific Items in the List

We can also add the elements of the “list” and “int” using an addition operator by accessing specific items in the list. This method does not add all the list items to an integer; instead, it adds only a specific list element with the given integer value.

Code:

list_value = [44, 23, 45]
output = 56 + list_value[0]
print(output)

In the above code:

  • The “list_value[0]” is used to get the list value placed at index position “0” which is “44”.
  • After that, the integer value “56” has been added to a specific index value using the addition operator “+”.

Output:

The above output calculates the integer value and list value successfully using a specific index value.

These are all the reasons and solutions to fix this TypeError.

Conclusion

The “TypeError: unsupported operand type(s) for +:int and list” occurs when a user uses the addition operator “+” to add integers and lists. To resolve this error, various solutions are used in Python, such as using “for” Loop, using List Comprehension, and accessing specific items in the list. The “for” loop and list comprehension method have similar functionality, i.e., adding integer values to each list element. This article presented a detailed guide on rectifying Python’s “unsupported operand type(s) for +:int and list” error.