

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do we compare the elements of two lists in Python?
The method cmp() compares elements of two lists. If elements are of the same type, it performs the compare and returns the result. If elements are different types, it checks to see if they are numbers. If they are numbers, it performs type coercion if necessary and compares. If either element is a number, then the other element is "larger" (numbers are "smallest"). Otherwise, types are sorted alphabetically by name.
If we reached the end of one of the lists, the longer list is "larger." If we exhaust both lists and share the same data, the result is a tie, meaning that 0 is returned.
example
list1 = [123, 'xyz'] list2 = [456, 'abc'] print(cmp(list1, list2)) print(cmp(list2, list1)) list2 = [123, 'xyz'] print(cmp(list1, list2))
Output
This will give the output −
-1 1 0
- Related Questions & Answers
- How do we compare two lists in Python?
- How do we compare two tuples in Python?
- How do we compare two dictionaries in Python?
- How to compare two lists in Python?
- How do we compare two arrays in Java?
- How do we compare Python Dates?
- How do we define lists in Python?
- Adding two Python lists elements
- How do we compare the members of enums in Java?
- How to compare two lists for equality in C#?
- Python program to print all the common elements of two lists.
- How do we compare String in Java
- How do we access elements from the two-dimensional array in C#?
- How do you add two lists in Java?
- Intersection of Two Linked Lists in Python
Advertisements