Open In App

cmp(list) method in Python

Last Updated : 08 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)


Output

Comparison 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: filecmp.cmp() method
Filecmp module in Python provides functions to compare files and directories. This module comes under Python’s standard utility modules. This module also consider the properties of files and directories for comparison in addition to data in them. filecmp.cmp() method in Python is used to compare two files. This method by default performs shallow co
3 min read
Python | cmp() function
cmp() method in Python 2.x compares two integers and returns -1, 0, 1 according to comparison. cmp() does not work in python 3.x. You might want to see list comparison in Python. NoteThe cmp function was a built-in function in Python 2 for comparing the values of two objects. It has been removed in Python 3 and replaced with the == and is operators
2 min read
Python | Convert list of string to list of list
Many times, we come over the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to la ist to perform tasks is quite common in web development. Let's discuss certain ways in which this
7 min read
Python | Convert list of tuples to list of list
This is a quite simple problem but can have a good amount of application due to certain constraints of Python language. Because tuples are immutable, they are not easy to process whereas lists are always a better option while processing. Let's discuss certain ways in which we can convert a list of tuples to list of list. Method #1: Using list compr
8 min read
Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isdigit() This task can be performed using a combinat
6 min read
Class Method vs Static Method vs Instance Method in Python
Three important types of methods in Python are class methods, static methods, and instance methods. Each serves a distinct purpose and contributes to the overall flexibility and functionality of object-oriented programming in Python. In this article, we will see the difference between class method, static method, and instance method with the help o
5 min read
Python | Maximum sum of elements of list in a list of lists
Given lists in a list, find the maximum sum of elements of list in a list of lists. Examples: Input : [[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]] Output : 33 Explanation: sum of all lists in the given list of lists are: list1 = 6, list2 = 15, list3 = 33, list4 = 24 so the maximum among these is of Input : [[3, 4, 5], [1, 2, 3], [0, 9, 0]] Outpu
4 min read
Sort the values of first list using second list in Python
Given two lists, sort the values of one list using the second list. Examples: Input : list1 = ["a", "b", "c", "d", "e", "f", "g", "h", "i"] list2 = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]Output : ['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g'] Input : list1 = ["g", "e", "e", "k", "s", "f", "o", "r", "g", "e", "e", "k", "s"] list2 = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]O
6 min read
Python List Comprehension | Segregate 0's and 1's in an array list
You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. Examples: Input : arr = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0] Output : [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] We have existing solution for this problem please refer Segregate 0s and 1s in an array link. We can solve this problem quickly in Python usi
2 min read
Python | Pair and combine nested list to tuple list
Sometimes we need to convert between the data types, primarily due to the reason of feeding them to some function or output. This article solves a very particular problem of pairing like indices in list of lists and then construction of list of tuples of those pairs. Let's discuss how to achieve the solution of this problem. Method #1 : Using zip()
10 min read
Article Tags :
Practice Tags :