0

cmp(list1,list2) is not supported in Python 3.x while it's supported in Python 2.x.Is there any alternative to cmp() function in Python 3.x?

Edward795
  • 71
  • 1
  • 7
  • Depending on exactly what you doing / using it for, there's also [`functools.cmp_to_key()`](https://docs.python.org/3/library/functools.html#functools.cmp_to_key). – martineau Nov 29 '19 at 13:57

1 Answers1

1

As part of the move away from cmp-style comparisons, the cmp() function was removed in Python 3.

If it is necessary (usually to conform to an external API), you can provide it with this code:

def cmp(x, y):
    """
    Replacement for built-in function cmp that was removed in Python 3

    Compare the two objects x and y and return an integer according to
    the outcome. The return value is negative if x < y, zero if x == y
    and strictly positive if x > y.
    """

    return (x > y) - (x < y)
ncica
  • 7,015
  • 1
  • 15
  • 37