You are currently viewing Python Tuple Comparison

Python tuples can be compared using the comparison operators <, <=, >, >=, ==, and != to determine their relative ordering. The comparison is performed element by element, starting from the first element and continuing to subsequent elements until a difference is found or all elements have been compared.

Advertisements

The following rules apply when comparing tuples:

  • The comparison starts with the first element of each tuple.
  • If the first elements of the tuples are different, the comparison result is determined based on the comparison between those elements.
  • If the first elements are equal, the comparison moves on to the second element, and so on, until a difference is found or all elements have been compared.
  • If all corresponding elements are equal, the comparison result is based on the lengths of the tuples. A shorter tuple is considered less than a longer tuple.

1. Quick Examples of Comparing the Tuples

If you are in a hurry, below are some quick examples of the tuple comparison.


# Quick examples of comparing the tuple 

# Example 1: Tuple inequality comparison
# Using the != operator
tuple1 = (2, 4, 6)
tuple2 = (2, 4, 6)
tuple3 = (1, 3, 5)
result = tuple1 != tuple2

# Example 2: Using the != operator
result = tuple1 != tuple3

# Example 3: Using the > operator
tuple1 = (2, 4, 6)
tuple2 = (1, 3, 5)
result = tuple1 > tuple2

# Example 4: Using the < operator
result = tuple1 < tuple2

# Example 5: Using the <= operator
tuple1 = (2, 4, 6)
tuple2 = (1, 3, 5)
result = tuple1 <= tuple2

# Example 6: Using the >= Operator
result = tuple1 >= tuple2

# Example 7: Using the == operator
tuple1 = (2, 4, 6)
tuple2 = (2, 4, 6)
tuple3 = (1, 3, 5)
result = tuple1 == tuple2

# Example 8: Using the == operator
result = tuple1 == tuple3

2. Tuple Inequality Comparison in Python

Tuple inequality comparison can be done using the != operator. It allows you to check if two tuples are not equal. The comparison is performed lexicographically, similar to tuple equality comparison. In the below example, tuple1 and tuple2 have the same elements, so the comparison tuple1 != tuple2 returns False because they are equal. On the other hand, tuple1 and tuple3 have different elements, so the comparison tuple1 != tuple3 returns True.

This operator is used to determine if two tuples are not equal. If any element of the two tuples is different, the result will be True. However, if all elements are the same, the result will be False.


# Initialize the tuples
tuple1 = (2, 4, 6)
tuple2 = (2, 4, 6)
tuple3 = (1, 3, 5)

# Tuple inequality comparison
# Using the != operator
# to compare given tuples
result = tuple1 != tuple2
print(result) 

# Output:
# False

# Using the != operator
result = tuple1 != tuple3
print(result)

# Output:
# True

2.1 Using the > Operator

You can also use the > (greater than) operator to compare two tuples tuple1 and tuple2. This comparison is performed lexicographically, element by element. The first elements of the tuples are compared: 2 and 1. Since 2 > 1, the comparison returns True.


# Initialize the tuple
tuple1 = (2, 4, 6)
tuple2 = (1, 3, 5)

# Using the > operator to compare
# given tuples
result = tuple1 > tuple2
print ("Tuple1 is greater than Tuple2:", result)

Yields below output.

python tuple comparison

2.2 Using the < Operator

Using the < (less than) operator you can compare two tuples tuple1 and tuple2. This comparison is performed lexicographically, element by element. The first elements of the tuples are compared: 2 and 1. Since 2 is not less than 1, the comparison returns False.


# Using the < operator
# to compare given tuples
result = tuple1 < tuple2
print ("Tuple1 is less than Tuple2:", result)

Yields below output.

python tuple comparison

2.3 Using the <= Operator

You can compare two tuples, tuple1 and tuple2, using the <= operator. It checks if tuple1 is less than or equal to tuple2. For example, the result of the comparison is False. The first element of tuple1 (2) is greater than the first element of tuple2 (1), so the comparison stops there and determines that tuple1 is not less than or equal to tuple2.


# Initialize the tuple
tuple1 = (2, 4, 6)
tuple2 = (1, 3, 5)

# Using the <= operator
# to compare given tuples
result = tuple1 <= tuple2
print ("Tuple1 is less than equal Tuple2:", result)

# Output:
# Tuple1 is less than equal Tuple2: False

2.4 Using the >= Operator

You can also compare two tuples, tuple1 and tuple2, using the >= operator. It checks if tuple1 is greater than or equal to tuple2. In this case, the result of the comparison is True. The first element of tuple1 (2) is greater than the first element of tuple2 (1).


# Initialize the tuple
tuple1 = (2, 4, 6)
tuple2 = (1, 3, 5)

# Using the >= Operator
# to compare given tuples
result = tuple1 >= tuple2
print ("Tuple1 is grater than equal Tuple2:", result)

# Output:
# Tuple1 is grater than equal Tuple2: True

3. Tuple Equality Comparison in Python

Alternatively, you can compare tuples using equality == operator. The == operator checks if the corresponding elements of the given tuples are equal. If all elements of the tuples are equal, the result is True; otherwise, it is False. In the below example, the tuples tuple1 and tuple2 have the same elements in the same order, so the comparison tuple1 == tuple2 evaluates to True.


# Initialize the tuple
tuple1 = (2, 4, 6)
tuple2 = (2, 4, 6)
tuple3 = (1, 3, 5)

# Using the == operator
# to compare given tuples
result = tuple1 == tuple2
print ("Tuple1 is equality Tuple2:", result)

# Output:
# Tuple1 is equality Tuple2: True

Below the example, you can compare tuple1 and tuple3 use the == operator, which checks if the corresponding elements of the tuples are equal. In this case, tuple1 contains the elements (2, 4, 6), and tuple3 contains the elements (1, 3, 5). When you compare tuple1 == tuple3, the first element of tuple1 is 2, and the first element of tuple3 is 1. Since these elements are not equal, the comparison stops at this point, and the result is False.


# Using the == operator
result = tuple1 == tuple3
print ("Tuple1 is equality Tuple3:", result)

# Output:
# Tuple1 is equality Tuple3: False

4. Conclusion

In this article, I have explained Python comparison operators <, <=, >, >=, ==, !=, and using these operators how we can compare two tuples/more than two tuples with examples.

Happy Learning !!