1

I was writing some code manipulating some lists in python where I ran into this

    #case1
    list1 = [2, 3, 4, 5] 
    list2 = [2, 3, 4, 6]
    #list1 > list2 is False
    
    #case2
    list3 = [1, 2, 3, 10] 
    list4 = [2, 4, 4, 3] 
    #list3 > list4 is also False

From case1 kinda makes sense cause the sum(list1) > sum(list2) but in case2 the sum(list3) > sum(list4) but still list4 is greater than list3. How does python compare two lists?

  • Does this answer your question? [Comparing two lists using the greater than or less than operator](https://stackoverflow.com/questions/13052857/comparing-two-lists-using-the-greater-than-or-less-than-operator) – TheFungusAmongUs Jul 29 '22 at 13:29

3 Answers3

1

list is not compared based on sum. it is compared based on index. in the first case, only 5<6 and others are the same so it returns false. but in 2nd case, 1<2 so it is returning false

this link will help: Comparing two lists using the greater than or less than operator

Yash
  • 1,271
  • 1
  • 7
  • 9
1

From the Python documentation:

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.

jrmylow
  • 679
  • 2
  • 15
-2

Only the first elements of the lists are compared to determine true or false. All the remaining elements can be anything.