
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
- Python Useful Resources
- Python - Questions and Answers
- Python - Quick Guide
- Python - Tools/Utilities
- Python - Useful Resources
- Python - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python List cmp() Method
Description
Python list method cmp() compares elements of two lists.
Syntax
Following is the syntax for cmp() method −
cmp(list1, list2)
Parameters
list1 − This is the first list to be compared.
list2 − This is the second list to be compared.
Return Value
If elements are of the same type, perform the compare and return the result. If elements are different types, check to see if they are numbers.
- If numbers, perform numeric coercion if necessary and compare.
- 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
The following example shows the usage of cmp() method.
#!/usr/bin/python list1, list2 = [123, 'xyz'], [456, 'abc'] print cmp(list1, list2) print cmp(list2, list1) list3 = list2 + [786]; print cmp(list2, list3)
When we run above program, it produces following result −
-1 1 -1
python_lists.htm
Advertisements