TypeError Unsupported Operand Type(s) for list and list

Sometimes, when you are dealing with lists in Python and when you try to subtract two list using the subtraction operator, you got TypeError: unsupported operand type(s) for list and list error. The reason for getting this error is that in Python we are not allowed to subtract the lists directly using the subtraction operator. However, there are different methods to subtract one list from another which we will discuss in the short article.

TypeError: Unsupported Operand Type(s) for -: ‘list’ and ‘list’
TypeError: Unsupported Operand Type(s) for -: ‘list’ and ‘list’

Solve TypeError: unsupported operand type(s) for -: list and list

As we know that the error, TypeError: unsupported operand type(s) for -: list and list occurs because we tried to subtract one list from another using the subtraction operator which is not allowed in Python. There are different methods to subtract one list from another but not using the subtraction operator directly.

Let us first take an example and see why we are getting TypeError: unsupported operand type(s) for -: list and list error:

# creating lists
list1 = [2, 3]
list2 = [3, 2]

# subtracting the list
result = list1 - list2 

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_6238/4227038197.py in <module>
      4 
      5 # subtracting the list
----> 6 result = list1 - list2

TypeError: unsupported operand type(s) for -: 'list' and 'list'


As you can see, when we tried to subtract list2 from list1, we got the error.

You also get a similar error when you tried to divide two lists as shown below:

# creating lists
list1 = [2, 3]
list2 = [3, 2]

# subtracting the list
result = list1 / list2 

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_6238/2439444741.py in <module>
      4 
      5 # subtracting the list
----> 6 result = list1 / list2

TypeError: unsupported operand type(s) for /: 'list' and 'list'

Notice that the error is the same. The only difference is the symbol of division.

Let us now jump into possible solutions and let us learn how we can subtract and divide two lists in Python without getting any errors:

Using NumPy Array

As we discussed, we cannot directly subtract one list from another using the subtraction operator. One method to get rid of the error is to convert the lists into NumPy arrays and then use the subtraction operator to subtract the lists.

# importng the numpy array
import numpy as np

# converting the lists into array
array1 = np.array([2, 3])
array2 = np.array([3, 2])

# subtracting the arrays
result = array1 - array2

# results
print(result) 

Output:

[-1  1]

As you can see, we were able to subtract the lists without getting any errors.

In a similar way, you can also divide the lists by converting them into NumPy arrays without getting any errors as shown below:

# importng the numpy array
import numpy as np

# converting the lists into array
array1 = np.array([2, 3])
array2 = np.array([3, 2])

# subtracting the arrays
result = array1 / array2

# results
print(result) 

Output:

[0.66666667 1.5       ]

Notice that the division was successful without getting TypeError: unsupported operand type(s) for /: list and list errors.

Using List Comprehension

In solution one, you can notice that when we converted the lists into numpy array and subtracted, we actually perform the subtraction operation on each of the elements of the list. However, you might not want such subtraction.

If you want to eliminate the elements that exist in list2 from list1 by performing a subtraction operation, then we can use the list comprehension methods to perform.

# creating the lists 
list1 = [1, 2, 3]
list2 = [2, 3, 4]

# using list comprehension
result = [x for x in list1 if x not in list2]

# printing results
print(result)

Output:

[1]

As you can see, the only element that does not exist in list 2 was element 1 and we got it. This method is also known as the filtering method where we are actually filtering and getting the list of elements that do not exist in list2:

Using set() method

Another way to subtract the elements of one list from another is to convert the lists into sets and then perform the subtraction operation as shown below:

# lists in python
list1 = [1, 2, 3]
list2 = [2, 3, 4]

# sutracting the list 
result = set(list1) - set(list2)

# convert the result back to list
result = list(result)

# printing
print(result)

Output:

[1]

As you can see, we were able to subtract the elements from the list.

Using the zip() Method

Another method through which we can subtract lists element-wise is using the zip() method. Let us take an example and see how we can use the zip() method to perform subtraction.

# lists in python
list1 = [1, 2, 3]
list2 = [2, 3, 4]
 
# creating an empty list
result = []
 
# using for loop and zip method
for i, j in zip(list1,list2):
 
    result.append(i - j)

# printing
print(result)

Output:

[-1, -1, -1]

As you can see, we got the result as each element being subtracted from lists.

Using for loop

We can also use the for loop in Python to subtract elements from the list. We will use the for loop to iterate through the list and subtract each element.

# lists in python
list1 = [1, 2, 3]
list2 = [2, 3, 4]

# creating an emtpy list
subtracted = list()

# using for loop 
for i in range(len(list1)):
    item = list1[i] - list2[i]
    subtracted.append(item)

# printing
print(subtracted)

Output:

[-1, -1, -1]

We can use the same method to perform the division operation as well to get rid of TypeError: unsupported operand type(s) for /: list and list error as shown below:

# lists in python
list1 = [1, 2, 3]
list2 = [2, 3, 4]

# creating an emtpy list
subtracted = list()

# using for loop 
for i in range(len(list1)):
    item = list1[i] / list2[i]
    subtracted.append(item)

# printing
print(subtracted)

Output:

[0.5, 0.6666666666666666, 0.75]

Notice that we were able to perform the division operation on two lists without any error using the for-loop method.

Using the lambda function

Another method to solve TypeError: unsupported operand type(s) for -: list and list error is to use the lambda function to perform the subtraction operation as shown below:

# lists in python
list1 = [1, 2, 3]
list2 = [2, 3, 4]

# using lambda function
subtracted_list = list(map(lambda x, y: x-y, list1, list2))
print(subtracted_list)

Output:

[-1, -1, -1]

In a similar way, we can use the lambda function to perform division as well so that we will no more get TypeError: unsupported operand type(s) for /: list and list error.

# lists in python
list1 = [1, 2, 3]
list2 = [2, 3, 4]

# using lambda function
subtracted_list = list(map(lambda x, y: x/y, list1, list2))
print(subtracted_list)

Output:

[0.5, 0.6666666666666666, 0.75]

Using the enumrate() method

The enumerate function in Python converts a data collection object into an enumerate object. Enumerate returns an object that contains a counter as a key for each value within an object, making items within the collection easier to access. We can use the enumerate method for the subtraction of lists in Python.

# lists in python
list1 = [1, 2, 3]
list2 = [2, 3, 4]

# using enumerate method
subtracted_list = [element - list2[i] for i, element in enumerate(list1)]

print(subtracted_list)

Output:

[-1, -1, -1]

In a similar way, you can use this method for division as well. You can try it by yourself.

Using the sub() operator

Another alternative method to perform subtraction on lists is using the sub() method. We need to import the operator module in order to access the sub() method.

# imorting operator
import operator

# lists in python
list1 = [1, 2, 3]
list2 = [2, 3, 4]

# subtracting 
subtracted_list = list(map(operator.sub, list1, list2))

print(subtracted_list)

Output:

[-1, -1, -1]

You can use the same method to perform division on lists as well.

Summary

In this short article, we discussed the reasons for getting TypeError: unsupported operand type(s) for -: list and list error. We learned that the error occurs when we tried to perform the subtraction operation directly on two lists. However, we can perform the subtraction on lists using various methods which we covered in this article.

Leave a Comment