Solution for “TypeError: unsupported operand type(s) for /: list and int” in Python

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

Many new Python developers usually get the TypeError: unsupported operand type(s) for /: list and int in their programs. This error is a common mistake and easy to fix. This article will show you the cause and solution for it! Slide down to learn!

The cause of the TypeError: unsupported operand type(s) for /: list and int in Python

The error occurs when you try to make a list divided by an integer in Python. 

Here is the sample of code that leads to this issue:

# Create a list: lstNumber
lstNumber = [2, 5, 7, 3, 12]

# Create an int: iNumber
iNumber = 3

# list / int
result = lstNumber / iNumber

An error message will appear:

  result = lstNumber / iNumber
TypeError: unsupported operand type(s) for /: 'list' and 'int'

Please notice that list and int are 2 separate data types. Therefore, you can not divide a list by an int. However, you are allowed to divide each element in the list by an int, as long as the element stays as a numeric type.

You can check the data types of the variables in your Python program by using the type() function. Put your variable in this function as its parameter like so:

type([variable])

See the code below:

lstNumber = [2, 5, 7, 3, 12]
print("Data type of lstNumber: ", type(lstNumber))

# Create an int: iNumber
iNumber = 3
print("Data type of iNumber: ", type(iNumber))

The output will be:

Data type of lstNumber:  <class 'list'>
Data type of iNumber:  <class 'int'>

Solution for this error

As mentioned, there is no way you can divide a list by an int. But you can divide each element in the list by the int

See the code sample below:

# Create a list: lstNumber
lstNumber = [24, 5.4, 9, 3.6, 12]
print("The list: ", lstNumber)

# Create an int: iNumber
iNumber = 3
print("The int: ", iNumber)

# Create a new list to store the result
lstResult = []

# Every element in list / int
for element in lstNumber:
    result = element / iNumber
    lstResult.append(result)

print("list / int = ", lstResult)

The output will be:

The list:  [24, 5.4, 9, 3.6, 12]
The int:  3
list / int =  [8.0, 1.8, 3.0, 1.2, 4.0]

Make sure that the elements must be 1 of the numeric types, which are float, int, and so on. An error will occur if an element in the list is not the numeric type.

For example, there is another list as an element in your existing list:

# Create a list: lstNumber
lstNumber = [24, 4, [3]]

# Create an int: iNumber
iNumber = 3

# Create a new list to store the result
lstResult = []

# Every element in list / int
for element in lstNumber:
    result = element / iNumber
    lstResult.append(result)

The error will still occur because you divide the list [3] by the int iNumber.

  result = element / iNumber
TypeError: unsupported operand type(s) for /: 'list' and 'int'

Summary

Never divide a list by an int, or else you will get the error message: “TypeError: unsupported operand type(s) for /: list and int” in Python. The way is to divide every element in the list by the int. You need to make sure that all the elements are in numeric data types!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *