Python List cmp () method
cmp () method is used to compare two elements of the list.
cmp () method syntax:
- list1 - the comparison list.
- list2 - the comparison list.
If you compare the elements of the same type, then compare its value, returning the result.
If the two elements are not the same type, check whether they are digital.
- If it is digital, perform the necessary digital casts, and then compare.
- If there is one element of a number, the other elements of the "big" (the number is the "smallest")
- Otherwise, type the name alphabetically by comparison.
If there is a list of the first to reach the end, the other a longer list of "big."
If we run out of elements of the two lists and all the elements are equal, then the result is a draw, that returns a 0.
The following example shows cmp () function to use:
#!/usr/bin/python list1, list2 = [123, 'xyz'], [456, 'abc'] print cmp(list1, list2); print cmp(list2, list1); list3 = list2 + ; print cmp(list2, list3)
Examples of the above output results are as follows:
-1 1 -1