YouTip LogoYouTip

Att List Cmp

# Python2.x Python List cmp() Method [![Image 3: Python Lists](#) Python Lists](#) * * * ## Description The cmp() method is used to compare the elements of two lists. ## Syntax The cmp() method syntax: cmp(list1, list2) ## Parameters * list1 -- The list to compare. * list2 -- The list to compare. ## Return Value If the elements being compared are of the same type, their values are compared, and the result is returned. If the two elements are not of the same type, it checks if they are numbers. * If they are numbers, necessary numeric type coercion is performed, and then they are compared. * If one element is a number, the other element is considered "greater" (numbers are "smallest"). * Otherwise, they are compared based on the alphabetical order of their type names. 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, then it's a tie, meaning 0 is returned. ## Example The following example demonstrates the use of the cmp() function: #!/usr/bin/python list1, list2 = [123, 'xyz'], [456, 'abc']print cmp(list1, list2);print cmp(list2, list1); list3 = list2 + ;print cmp(list2, list3) The output of the above example is: -11-1 [![Image 4: Python Lists](#) Python Lists](#)
← Att List LenProp Webcontrol Listcontrol Se β†’