I am aware of the following:
[1,2,3]<[1,2,4]
isTrue
because Python does an element-wise comparison from left to right and3 < 4
[1,2,3]<[1,3,4]
isTrue
because2 < 3
so Python never even bothers to compare3
and4
My question is how does Python's behavior change when I compare two lists of unequal length?
[1,2,3]<[1,2,3,0]
isTrue
[1,2,3]<[1,2,3,4]
isTrue
This led me to believe that the longer list is always greater than the shorter list. But then:
[1,2,3]<[0,0,0,0]
isFalse
Can someone please explain how these comparisons are being done by Python?
My hunch is that element-wise comparisons are first attempted and only if the first n
elements are the same in both lists (where n
is the number of elements in the shorter list) does Python consider the longer list to be greater. If someone could kindly confirm this or shed some light on the reason for this behavior, I'd be grateful.