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.
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)
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 list contains just integers.
This is the case when all the elements in the list are of type integers and hence when 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 the list are similar and one list is larger(in size) than the other list, larger list is considered to be greater.
Code #1 : Demonstrating cmp() using only integers.
Comparison of list2 with list1 : 1 Comparison of list2 with list3(larger size) : -1 Comparison of list4 with list1(equal) : 0
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.
Comparison of list2 with list1 : 1 Comparison of list2 with list3(larger size) : -1 Comparison of list3 with list4 : -1
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
- Time Functions in Python | Set-2 (Date Manipulations)
- Send mail from your Gmail account using Python
- Python – The new generation Language
- Print Single and Multiple variable in Python
- Increment and Decrement Operators in Python
- str() vs repr() in Python
- Swap two variables in one line in C/C++, Python, PHP and Java
- Generate all permutation of a set in Python
- Class or Static Variables in Python
- trunc() in Python
- Division Operators in Python
- Interesting facts about strings in Python | Set 1
- When to use yield instead of return in Python?
- How to split a string in C/C++, Python and Java?
- Interesting facts about strings in Python | Set 2 (Slicing)
- Returning Multiple Values in Python
- String Template Class in Python
- Python List Comprehension and Slicing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.