'>' not supported between instances of 'list' and 'float'

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

'>' not supported between instances of 'list' and 'float' #

The Python "TypeError: '>' not supported between instances of 'list' and 'float'" occurs when we use a comparison operator between values of type list and float. To solve the error, access the list at a specific index or use a list comprehension to filter out items from the list.

typeerror not supported between instances of list and float

Here is an example of how the error occurs.

main.py
my_list = [1.1, 2.2, 3.3, 4.4, 5.5] my_float = 3.14 # ⛔️ TypeError: '>' not supported between instances of 'list' and 'float' print(my_list > my_float)

We used a comparison operator between values of incompatible types (list and float) which caused the error.

To solve the error, make sure the values you are comparing are of compatible types.

One way to solve the error is to access a specific item in the list.

main.py
my_list = [1.1, 2.2, 3.3, 4.4, 5.5] my_float = 3.14 print(my_list[0] > my_float) # 👉️ False

We accessed the list item at index 0 (the first item) and compared it to a float.

The values you are comparing have to be of compatible types, if the list contains numbers wrapped in a string, convert the string to a number using the float() or int() classes.

If you meant to filter out floats in a list based on a comparison, use a list comprehension.

main.py
my_list = [1.1, 2.2, 3.3, 4.4, 5.5] my_new_list = [x for x in my_list if x > 3.14] print(my_new_list) # 👉️ [3.3, 4.4, 5.5]

The example shows how to filter out all values in the list that are not greater than 3.14.

If you meant to compare the list's length, use the len() function.

main.py
my_list = [1.1, 2.2, 3.3, 4.4, 5.5] print(len(my_list) > 3.14) # 👉️ True print(len(my_list)) # 👉️ 5

The len() function returns the length (the number of items) of an object.

The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

If you meant to compare the sum of the items in the list to a float, use the sum() function.

main.py
my_list = [1.1, 2.2, 3.3, 4.4, 5.5] print(sum(my_list) > 10.5) # 👉️ True print(sum(my_list)) # 👉️ 16.5

The sum function takes an iterable, sums its items from left to right and returns the total.

The values on the left-hand and right-hand sides of the comparison operator need to be of compatible types.

If you aren't sure what type of object a variable stores, use the built-in type() class.

main.py
my_list = [1.1, 2.2, 3.3, 4.4, 5.5] print(type(my_list)) # 👉️ <class 'list'> print(isinstance(my_list, list)) # 👉️ True my_float = 3.14 print(type(my_float)) # 👉️ <class 'float'> print(isinstance(my_float, float)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

Use the search field on my Home Page to filter through my more than 1,000 articles.