How To Solve “TypeError: < Not Supported Between Instances Of List And Int" in Python

TypeError: < not supported between instances of list and int

To fix the “TypeError: < not supported between instances of list and int” error in Python, there are some solutions we have effectively tested. Follow the article to better understand.

What causes the “TypeError: < not supported between instances of list and int” error?

In Python, the ‘<‘ operator which means less than. The ‘<‘ operator operates between two operands and checks if the left operand is less than the right operand. If less, It returns True and False if the condition is not satisfied.

TypeError: Python throws this error when you perform an unsupported operation on an object with an invalid data type.

The error “TypeError: < not supported between instances of list and int” happens when you use the ‘<‘ operator to compare two values ​​that are not of the same data type.

Example: 

# Creat a list of students' marks
students = ["Tom", "Perter", "Jery"]
marks = [4, 10, 5]

avgMark = 5

for i in range(len(marks)):
  # TypeError: '<' not supported between instances of 'list' and 'int'
  if marks < avgMark: 
    print(f"{students[i]}: Failed")
  else:
    print(f"{students[i]}: Passed")

Output:

Traceback (most recent call last):
  File "code.py", line 8, in <module>
   if marks < avgMark: 
TypeError: '<' not supported between instances of 'list' and 'int'

How to solve this error?

Access each index of the list

To solve this error, please access each list index, and call each element out using the ‘<‘ operator to compare with integers. Replace the code if marks < avgMark with the code if marks[i] < avgMark

This ensures you will not have any errors.

# Create a list of students' marks
students = ["Tom", "Perter", "Jery"]
marks = [4, 10, 5]

avgMark = 5

for i in range(len(marks)):
  if marks[i] < avgMark: 
    print(f"{students[i]}: Failed")
  else:
    print(f"{students[i]}: Passed")

Output:

Tom: Failed
Perter: Passed
Jery: Passed

Using the list comprehension

marks = [4, 10, 5]
avgMark = 5

# Use the list comprehension
marksPassed = [m for m in marks if m >= avgMark]
print(marksPassed)
print("Number of students who have passed: ", len(marksPassed))

Output:

[10, 5]
Number of students who have passed:  2

Summary

If you have any questions about the error “TypeError: < not supported between instances of list and int” in Python, please leave a comment below. We will support your questions. Thank you for reading!

Maybe you are interested:

Leave a Reply

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