Join Regular Classroom : Visit ClassroomTech

cmp() function in Python List

It is a function in python 2.x that compares two list provided in the argument. If the first argument list is greater than the second argument list it returns 1, if not returns -1.

Syntax:

cmp(list1, list2)

Example 1:

# Python program to understand cmp() function in list
# www.codewindow.in

a = [1, 2, 1]
b = [1, 2, 2]
print(cmp(a, b))

Output:

-1

Explanation:

Here the third element in the first list is lower than the third element in the second tuple. Hence returned -1.

Example 2:

# Python program to understand cmp() function in list
# www.codewindow.in

c = ['a', 'b', 'e']
d = ['a', 'b', 'd']
print(cmp(c, d))

Output:

1

Explanation:

Here the third element of the first list is greater than the third element of the second list. Hence returned 1.


You Missed

Also Checkout