62

I noticed a piece of code recently directly comparing two lists of integers like so:

a = [10,3,5, ...]
b = [5,4,3, ...,]
if a > b:
     ...

which seemed a bit peculiar, but I imagined it would return True if all of list_a's elements are larger then list_b's and False if each element is equal or list_b's elements are larger then list_a's. So I tested it:

>>> a=[3,3,3,3]
>>> b=[4,4,4,4]
>>> a>b
False
>>> b>a
True

Ok that works. As does:

>>> b = [1,1,1,1]
>>> a = [1,1,1,1]
>>> a>b
False
>>> b>a
False

but when it gets more fuzzy:

>>> a=[1,1,3,1]
>>> b=[1,3,1,1]
>>> a>b
False
>>> b>a
True

or:

>>> a=[1,3,1,1]
>>> b=[1,1,3,3]
>>> a>b
True
>>> b>a
False

the results are a bit stranger. What is python actually doing? It seems that it's returning the result in favour of the first list in which the left most element is greater then the corresponding?

Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177

3 Answers3

71

From Comparing Sequences and Other Types in the Python tutorial:

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.

See also the Wikipedia article about lexicographical order.

aggieNick02
  • 2,557
  • 2
  • 23
  • 36
gefei
  • 18,922
  • 9
  • 50
  • 67
24

Since I didn't find the explanation of list/tuple comparison using "lexicographical ordering" particularly illuminating at first, here's an attempt to explain it "in my own words". First, here are some example lists that are referred to in the explanation below:

a = [1, 2, 3]
b = [1, 2, 10]
c = [1, 2, 3, 100]
d = [1, 2, 3]
e = [1, 2, 3, 4, 'a']
f = ['a', 'b', 'c']

The pair of items at each index are compared in turn. So, comparing a to b will result in 1 being compared to 1, 2 being compared to 2, and 3 being compared to 10.

The comparison of pairs will stop when either an unequal pair of items is found or--if the lists are different lengths--the end of the shorter list is reached.

For example, when comparing a and b, comparisons will stop when 3 and 10 are compared. When comparing b and c, comparisons will stop when 10 and 3 are compared.

As soon as an unequal pair is found, the overall result is the result of comparing the unequal items. This applies whether the lists are the same length or not--for example, list b is greater than list c because the 100 in c never comes into play.

For example, when comparing a to b, the overall result will be the result of comparing 3 to 10. a < b -> True because 3 is less than 10. a > b -> False because 3 is not greater than 10. a == b -> False because 3 does not equal 10.

If one of the lists is shorter and its N items are equal to the first N items of the longer list, as with a and c, the shorter list will be considered less than the longer list (so a is less than c).

Two lists will compare as equal only if they're the same length and all pairs of items compare as equal.

Note about types: if the items in a pair aren't comparable, the comparison will fail with a TypeError as usual. For example, comparing list a to f will fail when 1 is compared to 'a'. But also note that lists d and e can be compared since the 'a' in e is never compared to anything in d.

  • 1
    "The comparison of pairs will stop when either an unequal pair of items is found or--if the lists are different lengths--the end of the shorter list is reached." if this is true why does a= [1,2,3] b = [1,2,3,4] a – vivekv Nov 16 '20 at 08:41
  • 2
    This happens often of stack overflow, a good question and two answers, one so complex that only people knowing the answer understand it and one that is overly complicated sold as "simple" answer. Pro tip look into source code of list object under __ge__ definition. List with greater item is always bigger if items are the same then lengt takes precedence, if lists are the same but in different order then the one to have first bigger item is greater. – nethero Apr 05 '21 at 19:17
-1

The given answers don't account for duplicates in the larger list, you can iterate over the bigger list and slice it each time to compare it with the sub list, this will maintain the order as well as account for duplicates.

This code would work:

def check_sublist(List,Sublist)
    for i in range(len(List)):
        if List[i:] ==Sublist:
            return True
    return False

Yes this is not time efficient but this is the only solution I could think since using set() would not maintain the order

S3VuYWwK
  • 1
  • 1
  • How is it related to the question? The OP is asking about the list comparison using the `<` and `>` operators, there's nothing about finding sub-lists. Also, I'm not sure what you mean by the duplicates. If you mean the duplicated list items in any of compared lists, then comparison operators don't care about them at all, they simply compare every item of the first list with the corresponding item of the second list, one by one until it reaches the end of any of those lists. – David Ferenczy Rogožan Jun 16 '23 at 15:45