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.

Answer from gefei on Stack Overflow
Top answer
1 of 3
77

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.

2 of 3
27

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.

Discussions

How to compare two lists for the greater values in Python - Stack Overflow
The first list is a list of thresholds. The second list of smaller length than the first. When comparing the two lists, if the value in the second list is greater than the corresponding value in the first list for the consecutive number given as an input, then the function returns that index. More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 5, 2017
python - Comparing two list with greater than > or less than < - Stack Overflow
I have two lists that I am trying to compare with in python. One (list1) is a slope and then a horizontal line (picture a obtuse angle of around 130 degrees) the second is a linear funct... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How can I compare two lists in python and return matches - Stack Overflow
A note of caution, the list comprehension is not necessarily the faster option. For larger sets (where performance is most likely to matter) the bitwise comparison (&) or set(a).intersection(b) will be as fast or faster than list comprehension. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python: How do you write this following in greater than and less than form?
When you are doing 80 <= 90 you are just comparing 80 and 90. It is always true regardless of the score. More on reddit.com
๐ŸŒ r/learnpython
6
2
November 17, 2020
๐ŸŒ
Index.dev
index.dev โ€บ blog โ€บ compare-list-elements-python
How to Compare Two Elements of a List in Python (2026)
October 17, 2024 - This is the right tool when you ... gives you the full set of relational operators for this: == (equal), != (not equal), > (greater than), < (less than), >= (greater than or equal), and <= (less than or equal)....
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ python โ€บ comparing-two-lists-using-the-greater-than-or-less-than-operator-in-python.html
Comparing two lists using the greater than or less than operator in python
You can compare two lists in Python using the greater than (>) or less than (<) operators, but keep in mind that these operators are used to compare the lexicographical (dictionary) order of the lists, which may not always produce the desired results.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 46578573 โ€บ how-to-compare-two-lists-for-the-greater-values-in-python
How to compare two lists for the greater values in Python - Stack Overflow
October 5, 2017 - Particularly the code for comparing the two lists for the greater value. ... def compare_lists(lst_a, lst_b, num): count = 0 # number of consecutive times a < b result = None # last index value to be returned for index, value in enumerate(lst_a): try: if value < lst_b[index]: count += 1 else: count = 0 if count == num: result = index # if you need the first index, then set result = index - num + 1 break except IndexError: break return result a = [1, 2, 3, 4, 5] b = [10, 20, 30] print(compare_lists(a, b, 3))
๐ŸŒ
YouTube
youtube.com โ€บ moein instructor
Greater than operator in a list to do the comparison of two lists - Python by #Moein - YouTube
Click here for full courses and ebooks: Learn Python from Scratch: https://www.udemy.com/course/learn-python-from-scratch-master-of-python/?referralCode=AC56...
Published ย  February 3, 2022
Views ย  52
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-compare-two-lists-in-python
How to Compare Two Lists in Python | DigitalOcean
July 22, 2025 - Weโ€™ll explore practical techniques for different comparison goals, from simple equality checks to finding unique elements. Youโ€™ll learn how to check for exact equality (content and order) using the == operator, and how to verify if lists have the same elements regardless of order with sorted() ...
Find elsewhere
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ comparison-operator-for-two-lists-python
Python List Comparison Operators: A Comprehensive Guide - CodeRivers
February 22, 2026 - If, at the first position where the elements differ, the element in the first list is greater than the element in the second list, then the first list is considered greater than the second list. list1 = [2, 1, 3] list2 = [1, 3, 2] print(list1 > list2) # Output: True ยท The less than or equal ...
๐ŸŒ
Quora
quora.com โ€บ How-do-I-compare-two-lists-in-Python
How to compare two lists in Python - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ list_cmp.htm
Python List cmp() Method
Following is the syntax for the Python List cmp() method โˆ’ ... The method returns 1 if the first list is greater than the second list and -1 if its smaller; 0 is returned when both the lists are equal.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-check-values-list-greater-given-value
Check if all the values in a list that are greater than a given value - Python - GeeksforGeeks
April 22, 2025 - Explanation: filter() with a lambda filters elements in a greater than b. If the filtered list's length equals the original list's length, it returns True otherwise, False. If we working with a large list of numeric values, we can use numpy, which is optimized for vectorized operations and can perform checks faster than standard Python loops.
๐ŸŒ
Blogger
nrthugu.blogspot.com โ€บ 2018 โ€บ 12 โ€บ comparing-two-lists-using-greater-than.html
Comparing two lists using the greater than or less than operator
December 8, 2018 - 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? ... 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.
๐ŸŒ
Statistics Globe
statisticsglobe.com โ€บ home โ€บ python programming language for statistics & data science โ€บ compare two lists element-wise in python (2 examples)
How to Compare Two Lists Element-Wise in Python (2 Examples)
November 6, 2023 - List1's element is {a}.") elif a == b: print("Equal") elif a > b: print("List1 is greater") else: print("List3 is greater") # Equal # Equal # Equal # Equal # List1 is greater # Element 5: List1 has a missing element. List3's element is 6. As the loop progresses with the help of enumerate, it checks for missing elements in either list, indicating which list lacks an element and at which position. If both lists have elements at a particular position, they are directly compared, and the relationship is printed out. The result provides a comprehensive understanding of the relative contents and sizes of the two lists.
๐ŸŒ
LARG*net
largnet.ca โ€บ largblog โ€บ pythoncomparelists
Python compare two lists or more. โ€” LARG*net - London and Region Global Network
November 2, 2022 - The index increases when list1 has been checked against all other lists so it moves on to compare list2 and list3. Thereโ€™s also no need for list 3 to go back since it has already been checked against all other lists. The list of feeds can be as long as you please and you end up with any overlaps that exist. This first step of analysis tells us that about 5,000 out of >100,000 possible entries are on more than 1 list.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python list comparison
Python List Comparison - Scaler Topics
May 4, 2023 - The function can also compare two items and return a result based on the inputs provided. This return value can be one of three things: 1, 0 or -1. For instance, if l1 and l2 are two lists, then value 1 is returned if l1 (list 1) is greater than l2 (or list2).
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-23099.html
Compare two lists. Count larger values
December 11, 2019 - Hi all I have two lists A and B and I want to compare them element-wise, to check how more often (as percentage) list A has a larger value than list B. In R that would have been a for loop, but I think in python are smarter ways to work with lists? ...
๐ŸŒ
Miguendes
miguendes.me โ€บ python-compare-lists
The Best Ways to Compare Two Lists in Python
July 2, 2022 - The example below starts off by setting up the two lists we want to compare. We then pass it to the deepdiff.DeepDiff constructor which returns the difference. That's great, the returned value is much more informative than a simple boolean.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-compare-two-lists-in-python
How to compare two lists in Python?
def compare_lists_sorted(list1, list2): list1.sort() list2.sort() if list1 == list2: return "Equal" else: return "Not equal" numbers1 = [1, 2, 3] numbers2 = [2, 1, 3] print("After sorting:", compare_lists_sorted(numbers1, numbers2)) numbers3 = [1, 2, 3] numbers4 = [1, 2, 4] print("Different elements:", compare_lists_sorted(numbers3, numbers4))