Comparing lists

  • Thread starter Francesco Bochicchio
  • Start date
F

Francesco Bochicchio

Hi all,

anybody can point me to a description of how the default comparison of
list objects (or other iterables) works?

Apparently l1 < l2 is equivalent to all ( x < y for x,y in
zip( l1, l2) ), has is shown in the following tests, but I can't find
it described anywhere:
[1,2,3] < [1,3,2] True
[1,2,3] < [1,2,4] True
[1,2,3] < [2,2,3] True
[1,2,3] < [0,1,3] False
[1,2,3] < [0,2,3] False
[1,2,3] < [1,1,3] False
[1,2,3] < [1,2,2]
False


Ciao
 
P

Peter Otten

Francesco said:
Hi all,

anybody can point me to a description of how the default comparison of
list objects (or other iterables) works?

Apparently l1 < l2 is equivalent to all ( x < y for x,y in
zip( l1, l2) ), has is shown in the following tests, but I can't find
it described anywhere:
[1,2,3] < [1,3,2] True
[1,2,3] < [1,2,4] True
[1,2,3] < [2,2,3] True
[1,2,3] < [0,1,3] False
[1,2,3] < [0,2,3] False
[1,2,3] < [1,1,3] False
[1,2,3] < [1,2,2]
False


Ciao

Quoting http://docs.python.org/reference/expressions.html

"""
Tuples and lists are compared lexicographically using comparison of
corresponding elements. This means that to compare equal, each element must
compare equal and the two sequences must be of the same type and have the
same length.
If not equal, the sequences are ordered the same as their first differing
elements. For example, cmp([1,2,x], [1,2,y]) returns the same as cmp(x,y).
If the corresponding element does not exist, the shorter sequence is ordered
first (for example, [1,2] < [1,2,3]).
"""
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top