Search code examples
python-3.xlistcoordinates

How does Python determine which coordinates is greater?


Say, I have two lists each containing just a pair of coordinates:

list1 = [2,1]

list2 = [1,2]

list3 = [2,3]

Why does list1 > list2 evaluate to True

but list2 > list3 evaluate to false?


Solution

  • It compares element by element until it finds two different values at which point it returns either True or False depending on which list contains the larger value. It is equivalent to the following:

    def compare(a, b):
        # Equivalent to a > b
        for i, j in zip(a, b):
            if i > j:
                return True
            elif i < j:
                return False
        return False