Copyright © tutorialspoint.com

Python List cmp() Function

previous next


Description:

This function compares elements of two lists.

Syntax:

cmp(list1, list2)

Parameters:

Here is the detail of parameters:

Return Value:

If elements are of the same type, perform the compare and return the result. If elements are different types, check to see if they are numbers.

If we reached the end of one of the lists, the longer list is "larger." If we exhaust both lists and share the same data, the result is a tie, meaning that 0 is returned.

Example:

#!/usr/bin/python

list1, list2 = [123, 'xyz'], [456, 'abc']

print cmp(list1, list2);
print cmp(list2, list1);
list3 = list2 + [786];
print cmp(list2, list3)

This will produce following result:

-1
1
-1

previous next

Copyright © tutorialspoint.com