4

I was fooling around and stumbled on something I don't understand...

Question 1:

a = [1,2,3]
b = [1,2,3,4]

len(a) < b

The result is True, but is this actually comparing the length of the two lists? It seems to be since this is also True...

a = [15,32,7]
len(a) < b

Question 2:

What happens when we try comparing integers with lists? Why are these all True (I'm assuming there's a general explanation...)...

3 < b
20 < b
float('inf') < b
None < b
(lambda x: (x**x)**x) < b

...and these False?

'poo' < b
'0' < b

3 Answers3

6

In Python 2.x, items of dissimilar types which are not directly comparable are compared using the name of their types. So all integers are less than all lists, because "int" is less than "list". For the same reason all strs are greater than all ints and floats.

This unintuitive behavior (which I assume was introduced so that items of like type sort together in a heterogeneous list) was removed in Python 3, which raises an exception for these comparisons.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • Applied for `(2, ,3, 4) < [2, 3, 4]` also **?** I means this is = `"tuple" < "list"` = `False`. – Grijesh Chauhan Oct 12 '13 at 17:31
  • Thanks, that's very helpful. So what about len(list1) < list2? Is it actually comparing list lengths as it seems to be? – myedibleenso Oct 12 '13 at 17:31
  • `len(list1)` is an integer, while `list2` is a list. The integer is always less than the list, as I stated. The lengths of the lists are not being compared. – kindall Oct 12 '13 at 17:33
  • 1
    @GrijeshChauhan: You might have caught my answer before I added "directly comparable" to it. Tuples and lists can be compared to each other because they are both sequences. – kindall Oct 12 '13 at 17:34
  • @kindall Yes but I just tried on my Python3.3 `(2, ,3, 4) < [2, 3, 4]` gives me `TypeError: Unoderable Types tuple() < list()`. – Grijesh Chauhan Oct 12 '13 at 17:38
  • kindall: len([15, 32, 7, 45, 61]) < [1, 2, 3, 4] is False – myedibleenso Oct 12 '13 at 17:39
  • @GrijeshChauhan: kindall explicitly said that this behaviour was removed in Python 3, and that it raises an exception. – DSM Oct 12 '13 at 17:39
  • @DSM Ok read again second para got it now, thanks! – Grijesh Chauhan Oct 12 '13 at 17:41
1

From the docs on data types:

Sequence objects may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.

And

Note that comparing objects of different types is legal. The outcome is deterministic but arbitrary: the types are ordered by their name. Thus, a list is always smaller than a string, a string is always smaller than a tuple, etc. 1 Mixed numeric types are compared according to their numeric value, so 0 equals 0.0, etc.

And, notably,

Footnotes 1 The rules for comparing objects of different types should not be relied upon; they may change in a future version of the language.

roippi
  • 25,533
  • 4
  • 48
  • 73
0

The other answers do a good job explaining what is happening, but the right way to compare the lengths is

len(a) < len(b)
James Robinson
  • 822
  • 6
  • 13
  • I don't think the OP had trouble comparing lengths. He wanted an explanation on why he was seeing the behavior he witnessed. This answer, while correct, is not really targeted to the OP's needs. –  Oct 12 '13 at 17:49
  • Thats pretty much what I thought too, except this line "but is this actually comparing the length of the two lists". So I thought I'd throw it out there just in case. – James Robinson Oct 12 '13 at 18:09