Python List cmp() method
Description
The cmp() method compares the elements of two lists.
Syntax
cmp() method syntax:
cmp(list1, list2)
Parameters
- list1 -- the list to compare.
- list2 -- the list to compare.
Return Value
If the compared elements are of the same type, their values are compared and the result is returned.
If the two elements are not of the same type, check whether they are numbers.
- If they are numbers, perform the necessary numeric coercion and then compare.
- If one element is a number, the other element is considered greater (numbers are the smallest)
- Otherwise, compare by the alphabetical order of the type names.
If one list reaches its end first, the other, longer list is considered greater.
If we exhaust the elements of both lists and all elements are equal, the result is a tie, that is, 0 is returned.
Example
The following example shows how to use the cmp() function:
#!/usr/bin/python
list1, list2 = [123, 'xyz'], [456, 'abc']
print cmp(list1, list2);
print cmp(list2, list1);
list3 = list2 + [786];
print cmp(list2, list3)
The output of the above example is:
-1
1
-1
Python Lists