In Python 2, why do lists compare as greater than numbers? And why tuples greater than lists?

  • A+
Category:Languages

Both [] > float('inf') and () > [] evaluate to True.

Is there any rationale behind it? I realize that it is Pythonic to not complain when comparing data structures of different types like this.

 


Alphabetical Order of the type name.

From the docs:

CPython implementation detail: Objects of different types except numbers are ordered by their type names;

>>> 0.0 < [] < ()  # Python2: float < list < tuple. True 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: