comparison of list using cmp or ==

You will very rarely need to use cmp. cmp has the same effect as testing <, == and >, but it is less readable.

In your case, use == as it will perform deep list equality testing.


If you are only interested in their equality, then I would say use the equality operator ==.

The cmp() function gives slightly different info, as the documentation describes:

cmp() - 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
  • strictly positive if x > y.

In your case, the "expected" result would be zero, a falsy value, which is not to intuitive if you are actually testing for equality.

Tags:

Python