C.I.Arjun
C.I.Arjun

Reputation: 77

I got a TypeError: '<' not supported between instances of 'str' and 'int'

In python while printing minimum value in a list using min() function gives me an error, when I try it in spyder Ide. But when I run the same code in pycharm it works well. What should I do to make it print on spyder Ide?

This is my code

lst = [101,754,'abcd','xyz','m']
Printing("Minimum value in List:", min(lst))

This gives an error:

TypeError: '<' not supported between instances of 'str' and 'int'

Upvotes: 1

Views: 1815

Answers (2)

tzot
tzot

Reputation: 96061

Most probably your spyder IDE defaults to Python 3 in your system, and your PyCharm project to Python 2.

In Python 3, ordering between str and int instances is undefined and throws an Exception. In Python 2, IIRC it returns True or False based on the addresses (id()) of the two objects.

Upvotes: 2

Vikhyat Agarwal
Vikhyat Agarwal

Reputation: 1843

Since some elements of the list are strings it can't find the minimum number in the list, since the min() function tries comparing the numbers with the string too.

Try this:

List = [101,754,'abcd','xyz','m']
numList = list(filter(lambda x: type(x)!=str, List)) # Creates a new list with only numbers
print("Minimum value in List:", min(numList))

Upvotes: 0

Related Questions