cmp(list) method in Python
Last Updated :
11 Jul, 2025
cmp(list) is a method specified in Number in Python 2. The comparison of integral numbers have been discussed using cmp(). But many a times, there is a need to compare the entire list that can be composed of similar or different data types. In this case, different case scenarios occur and having knowledge of them can at times prove to be quite handy.
Note: The cmp function in Python 2 is used to compare two objects and return a value indicating their relative order. In Python 3, the cmp function was removed and the __lt__, __le__, __gt__, and __ge__ methods were added to implement comparison operations.
This function takes 2 lists as input and checks if the first argument list is greater, equal or smaller than the second argument list.
Syntax:
cmp(list1, list2)
Parameters :
- list1 : The first argument list to be compared.
- list2 : The second argument list to be compared.
Returns : This function returns 1, if first list is "greater" than second list, -1 if first list is smaller than the second list else it returns 0 if both the lists are equal.
There are certain case scenarios when we need to decide whether one list is smaller or greater or equal to the other list.
Case 1: When the list contains just integers. This is the case when all the elements in the list are of type integers and hence when the comparison is made, the number-by-number comparison is done left to right, if we get a larger number at any particular index, we term it to be greater and stop the further comparisons. If all the elements in both list are similar and one list is larger(in size) than the other list, the larger list is considered to be greater.
Code #1: Demonstrating cmp() using only integers.
Python
# Python code to demonstrate
# the working of cmp()
# only integer case.
# initializing argument lists
list1 = [1, 2, 4, 3]
list2 = [1, 2, 5, 8]
list3 = [1, 2, 5, 8, 10]
list4 = [1, 2, 4, 3]
list5 = [7, 2, 5, 8]
# Comparing lists
print "Comparison of list2 with list1 : ",
print cmp(list2, list1)
# prints -1, because list3 has larger size than list2
print "Comparison of list2 with list3(larger size) : ",
print cmp(list2, list3)
# prints 0 as list1 and list4 are equal
print "Comparison of list4 with list1(equal) : ",
print cmp(list4, list1)
# prints -1 although list5 has larger size but when with comparison of list3(lesser size) then value dominates
print "Comparison of list3 with list5 (list5 has larger size with comparison of list3 has lesser size) :",
print cmp(list3, list5)
OutputComparison of list2 with list1 : 1
Comparison of list2 with list3(larger size) : -1
Comparison of list4 with list1(equal) : 0
Comparison of list3 with list5 (list5 has larger size with comparison of list3 has lesser size) : -1
Case 2: When list contains multiple datatypes. The case when more than one datatypes, eg. string is contained in the string, string is considered to be greater than integer, by this way, all datatypes are alphabetically sorted in case of comparison. Size rule remains intact in this case.
Code #2 : Demonstrating cmp() using multiple data types.
Python
# Python code to demonstrate
# the working of cmp()
# multiple data types
# initializing argument lists
list1 = [1, 2, 4, 10]
list2 = [1, 2, 4, 'a']
list3 = ['a', 'b', 'c']
list4 = ['a', 'c', 'b']
# Comparing lists
# prints 1 because string
# at end compared to number
# string is greater
print "Comparison of list2 with list1 : ",
print cmp(list2, list1)
# prints -1, because list3
# has an alphabet at beginning
# even though size of list2
# is greater, Comparison
# is terminated at 1st
# element itself.
print "Comparison of list2 with list3(larger size) : ",
print cmp(list2, list3)
# prints -1 as list4 is greater than
# list3
print "Comparison of list3 with list4 : ",
print cmp(list3, list4)
Output:
Comparison of list2 with list1 : 1
Comparison of list2 with list3(larger size) : -1
Comparison of list3 with list4 : -1
Similar Reads
Python List methods Python list methods are built-in functions that allow us to perform various operations on lists, such as adding, removing, or modifying elements. In this article, weâll explore all Python list methods with a simple example.List MethodsLet's look at different list methods in Python:append(): Adds an
3 min read
Python List remove() Method Python list remove() function removes the first occurrence of a given item from list. It make changes to the current list. It only takes one argument, element we want to remove and if that element is not present in the list, it gives ValueError.Example:Pythona = ['a', 'b', 'c'] a.remove("b") print(a
3 min read
Print lists in Python Printing a list in Python is a common task when we need to visualize the items in the list. There are several methods to achieve this and each is suitable for different situations. In this article we explore these methods.The simplest way of printing a list is directly with the print() function:Pyth
3 min read
Map vs List comprehension - Python List comprehension and map() both transform iterables but differ in syntax and performance. List comprehension is concise as the logic is applied in one line while map() applies a function to each item and returns an iterator and offering better memory efficiency for large datasets.List comprehensio
2 min read
Python List of Lists A list of lists in Python is a collection where each item is another list, allowing for multi-dimensional data storage. We access elements using two indices: one for the outer list and one for the inner list. In this article, we will explain the concept of Lists of Lists in Python, including various
3 min read
Python | sympy.lcm() method With the help of sympy.lcm() method, we can find the least common multiple of two numbers that is passed as a parameter in the sympy.lcm() method. Syntax : sympy.lcm(var1, var2) Return : Return value of least common multiple. Example #1 : In this example we can see that by using sympy.lcm() method,
1 min read