Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python List cmp() method

Python List cmp() method

Python Lists Python Lists


Description

The cmp() method compares the elements of two lists.

Syntax

cmp() method syntax:



cmp(list1, list2)

Parameters

Return Value

If the compared elements are of the same type, their values are compared and the result is returned.

If the two elements are not of the same type, check whether they are numbers.

If one list reaches its end first, the other, longer list is considered greater.

If we exhaust the elements of both lists and all elements are equal, the result is a tie, that is, 0 is returned.

Example

The following example shows how to use the cmp() function:



#!/usr/bin/python



list1, list2 = [123, 'xyz'], [456, 'abc']



print cmp(list1, list2);

print cmp(list2, list1);

list3 = list2 + [786];

print cmp(list2, list3)

The output of the above example is:



-1

1

-1

Python Lists Python Lists