How to Compare Lists Lexicographically in Python?

Python – Compare Lists Lexicographically

To compare lists lexicographically in Python, use Comparison Operators.

  • To check if two lists are equal, use equal to == operator.
  • To check if a list is greater than the other lexicographically, use greater than > operator.
  • To check if a list is less than the other lexicographically, use less than < operator.

Items from both the lists at their respective positions are compared one by one.

In this tutorial, we will go through each of the scenarios.

1. Check if Two Lists are Equal

list1 == list2 compares respective items from left to right in the list, and

  • If at any comparison, the items are are not equal, then the expression list1 == list2 returns False.
  • If respective items are equal till the end of both the lists, then list1 == list2 returns True

In the following example, we will compare two lists and check if they are equal using equal to operator.

Python Program

list1 = [2, 8, 4, 6]
list2 = [2, 8, 4, 6]

if list1 == list2:
    print('The two lists are equal.')
else:
    print('The two lists are not equal.')
Run Code Copy

Output

The two lists are equal.

2. Check if List 1 is Greater than List 2

When we give two lists as operands to Greater Than operator, items at respective positions are compared from left to right to check if item from list1 is greater than item from list2. During a comparison, if item from list1 equals item from list2, then comparison operation moves to next item. If item from list1 is greater than that of list2, comparison stops and True is returned. If item from list1 is less than that of list2, comparison stops and False is returned.

In this example, we will take two lists: list1 and list2; and check if list1 is greater than list2 lexicographically using greater than operator.

Python Program

list1 = [2, 8, 14, 16]
list2 = [2, 8, 4, 6]

if list1 > list2:
    print('list1 is greater than list2.')
else:
    print('list1 is not greater than list2.')
Run Code Copy

Output

list1 is greater than list2.

The first two items are same in list1 and list2. But for, third position, item in list1 is greater than item in list2. Therefore, list1 is greater than list2, lexicographically.

Now let us take lists such that at index=1, item of list1 is less than the item at list2. When we check if list1 is greater than list2, the comparison operation should return False.

Python Program

list1 = [12, 8, 14, 16]
list2 = [12, 28, 4, 6]

if list1 > list2:
    print('list1 is greater than list2.')
else:
    print('list1 is not greater than list2.')
Run Code Copy

Output

list1 is not greater than list2.

First item of list1 and first item of list2 are equal. Comparison moves to second item. Since, the second item in list1 is not greater than the second item in list2, list1 > list2 returns False.

3. Check if List 1 is Less than List 2

Less than comparison starts from left for each item in the lists. During comparison, for given index, if item from list1 equals item from list2, then comparison operation moves to next item. If item from list1 is less than that of list2, comparison stops and True is returned. If item from list1 is greater than that of list2, comparison stops and False is returned.

In this example, we will take two lists: list1 and list2; and check if list1 is less than list2 lexicographically using greater than operator.

Python Program

list1 = [2, 8, 1, 16]
list2 = [2, 8, 4, 6]

if list1 < list2:
    print('list1 is less than list2.')
else:
    print('list1 is not less than list2.')
Run Code Copy

Output

list1 is less than list2.

The first two items are same in list1 and list2. But for, third position, item in list1 is less than item in list2. Therefore, list1 is less than list2, lexicographically.

Now let us take lists such that at index=1, item of list1 is greater than the item at list2. When we check if list1 is less than list2, the comparison operation should return False.

Python Program

list1 = [12, 18, 14, 16]
list2 = [12, 8, 4, 6]

if list1 > list2:
    print('list1 is less than list2.')
else:
    print('list1 is not less than list2.')
Run Code Copy

Output

list1 is not less than list2.

First items are equal. So, comparison moves to second item. Since, the second item in list1 is not less than the second item in list2, list1 < list2 returns False.

Summary

In this tutorial of Python Examples, we learned how to compare two lists lexicographically in Python.

Code copied to clipboard successfully 👍