# Python number | cmp (list) method

|

Integer comparison was discussed using cmp () . But it is often necessary to compare the entire list, which may consist of the same or different data types. In this case, various scenarios arise, and knowing them can sometimes be quite useful.

This function takes 2 lists as input and checks if the first argument list is greater than, equal to, or less 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.

Exist certain scenarios where we need to decide whether one list is less than or greater than or equal to another list.

Case 1: when the list contains only integers.

This is the case when all the elements in the list have integer values and, therefore, when a comparison is performed, the number-by-number comparison is performed from left to right. If we get a larger number at any given index, we will call it large and stop further comparisons. If all the elements in both lists are similar, and one list is larger (in size) than the other, the larger list is considered large.

Code # 1: Demonstrate cmp () using only integers.

 ` # Python code for demonstration ` ` # cmp () work ` ` # 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 ` `] `   ` # List comparison ` ` print ` ` "Comparison of list2 with list1:" ` `, ` ` print ` ` cmp ` ` (list2, list1) `   ` # prints -1 because list3 is larger than list2 ` ` print ` ` "Comparison of list2 with list3 (larger size):" ` `, ` ` print ` ` cmp ` ` (list2, list3) `   ` # prints 0 because list1 and list4 are equal ` ` print ` ` "Comparison of list4 with list1 (equal):" , `` print cmp (list4, list1) `

Exit :

` Comparison of list2 with list1: 1 Comparison of list2 with list3 (larger size): -1 Comparison of list4 with list1 (equal): 0 `

Case 2: when the list contains several types of data.
A case where more than one data type, eg. the string is contained in the string, the string is considered to be greater than an integer, so all data types are sorted alphabetically in the case of a comparison. In this case, the size rule remains unchanged.

Code # 2: Demonstrate cmp () using multiple data types.

 ` # Python code for demonstration ` ` # cmp () operation ` ` # multiple data types `   ` # initializing argument lists ` ` list1 ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 4 ` `, ` ` 10 ` `] ` ` list2 ` ` = ` ` [` ` 1 , 2 , 4 , ’a’ ] `` list3 = [ ’a ’ , ’ b’ , ’c’ ] list4 = [ ’ a’ , ’c’ , ’ b’ ]     # List comparison # prints 1 because line # at the end compared to a number # line is greater print "Comparison of list2 with list1:" , print cmp (list2, list1)    # prints -1 because list3 # has an alphabet at the beginning # although the size of list2 # larger, Comparison # ends with 1 The element itself #. print "Comparison of list2 with list3 (larger size):" , print cmp (list 2, list3)   # prints -1 because 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 `

## Shop Best laptop for Fortnite

\$ Best laptop for Excel

\$ Best laptop for Solidworks

\$ Best laptop for Roblox

\$ Best computer for crypto mining

\$ Best laptop for Sims 4

\$ Best laptop for Zoom

\$499 Best laptop for Minecraft

\$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method