In Python 2 Built-In Tuple cmp() function is used to compare 2 elements in the given Tuple.
Syntax:
There are 2 parameters used in Tuple cmp() Function: tuple1 The first list to be compared and tuple2 The second list to be compared.
Example:
#!/usr/bin/python #Python 2 tuple1 = [123, 'xyz'] tuple2 = [456, 'abc'] print cmp(tuple1, tuple2) print cmp(tuple2, tuple1) tuple3 = tuple2 + [786]; print cmp(tuple2, tuple3)
Tuple cmp() Function Python 3
In Python 3, the cmp function does not exists and the function can be defined manually. Aftwards the same function can be used.
#!/usr/bin/python #Python 3 def cmp(a, b): return (a > b) - (a < b) tuple1 = [123, 'xyz'] tuple2 = [456, 'abc'] print (cmp(tuple1, tuple2)) print (cmp(tuple2, tuple1)) tuple3 = tuple2 + [786]; print (cmp(tuple2, tuple3))