The cmp() function was available in Python 2 and was used to compare two values. Based on the comparison, it returned a number indicating whether one value was smaller, equal, or greater than the other. However, this function was removed in Python 3, so it is no longer available in modern Python versions.
What Was the Python cmp() Function?
To understand its purpose, let’s see what this function actually did.
The cmp(a, b) function compared two values and returned:
-1ifa < b0ifa == b1ifa > b
In simple terms, it told you how two values relate to each other.
Important: This function is only available in Python 2 and has been completely removed in Python 3.
Syntax (Python 2 Only)
Here’s how the function was written and used.
cmp(a, b)
Parameters
| Parameter | Type | Description |
|---|---|---|
| a | Any | First value to compare |
| b | Any | Second value to compare |
Return Value
-1when the first value is smaller0when both values are equal1when the first value is greater
Example (Python 2)
Let’s look at a simple example to see how it behaved.print(cmp(5, 3)) # Output: 1
print(cmp(3, 5)) # Output: -1
print(cmp(4, 4)) # Output: 0
Explanation: The function returns a number based on how the two values compare.
How to Do This in Python 3
Since this function is no longer available, here’s the modern approach. Sincecmp() is not available in Python 3, you can use a simple expression to get similar behavior:
x = 5
y = 3
result = (x > y) - (x < y)
print(result) # Output: 1
Explanation:
(x > y) becomes 1 if true, otherwise 0
(x < y) becomes 1 if true, otherwise 0
Subtracting them gives the same result as cmp().
Why Was cmp() Removed?
There were a few reasons why Python moved away from this function.- Python now prefers direct comparison operators like
<,>, and== - Sorting is handled using
keyfunctions instead of comparison functions - Removing it simplified the language and avoided confusion between Python 2 and Python 3
How to Replicate cmp() Behavior in Python 3
Although thecmp() function is no longer available in Python 3, you can still achieve the same result using a simple expression.
The idea is to compare two values and convert the result into -1, 0, or 1.
x = 5
y = 3
result = (x > y) - (x < y)
print(result) # Output: 1
Explanation:
(x > y) returns True (1) if x is greater than y
(x < y) returns True (1) if x is less than y
Subtracting them gives:
1→ whenx > y0→ whenx == y-1→ whenx < y
cmp(x, y) function.
Using cmp()-like Logic in Sorting
If you previously usedcmp() for sorting, Python 3 provides a better approach using key functions.
data = [5, 2, 9, 1]
# Sorting using key instead of cmp
sorted_data = sorted(data)
print(sorted_data) # Output: [1, 2, 5, 9]
For custom sorting logic, you can use functools.cmp_to_key() to convert a comparison function into a key function.
from functools import cmp_to_key
def compare(a, b):
return (a > b) - (a < b)
data = [5, 2, 9, 1]
sorted_data = sorted(data, key=cmp_to_key(compare))
print(sorted_data) # Output: [1, 2, 5, 9]
Explanation:
This example demonstrates how to use comparison-based logic in Python 3, even though the cmp() function is no longer available.
The compare(a, b) function is designed to behave like the old cmp() function. It returns:
1whena > b0whena == b-1whena < b
However, Python 3 does not accept comparison functions directly in sorting. Instead, it requires a key function.
This is where cmp_to_key() is used. It converts the compare() function into a key-based format that works with sorted().
As a result, the list is sorted using the custom comparison logic, and the output is [1, 2, 5, 9].
Key Takeaways
Here are the main points to remember about the cmp() function.
cmp()was a built-in function in Python 2 used to compare two values.- It returned
-1,0, or1based on the comparison result. - This function has been completely removed in Python 3.
- In Python 3, similar behavior can be achieved using
(a > b) - (a < b). - For sorting, Python 3 uses
keyfunctions instead of comparison functions. - The
functools.cmp_to_key()utility can be used when comparison-style logic is required.