Compare List Function : cmp(list1,list2):
As the name advise, the method cmp() compares of two lists.
Where list1 -> this is
the first list to be compared and list2 -> this is the second list to be
compared.
What does cmp(list1,list2) method return ?
Well the return is
negative integer or zero or positive integer based on below condition:
·
If list1 < list2 : Return will
be negative integer
·
If list1 = list2 : Return will be
Zero
·
If list1 > list2 : Return will
be positive integer
# List
Compare Function examples
# 1.
list1 < list2 : return -1
list1 =
[1,2,3,4]
list2 =
[2,3,4,5]
print cmp(list1,list2)
>>>
-1
# 2.
list3 = list4 : return 0
list3 =
['a','b','c']
list4 =
['a','b','c']
print cmp(list3,list4)
>>>
0
# 3.
list5 > list6: return 1
list5 =[2,3,4]
list6=[1,0,2]
print cmp(list5,list6)
>>>
1
Length List Function :len(list):
As the name depicts , the length method returns total length of list.
#
Length Method Example:
L1 = [1,2,3,[3,5,5],(1,2,3),'abc','d','e']
print len(L1)
>>>
8
Maximum List Function: max (list):
Maximum method returns the item from list with maximum value.
#
Maximum item from list with max value
maxList
= [1,20,3,22,80]
print max(maxList)
>>>
80
Minimum List Function: min (list):
Maximum method returns the item from list with minimum value.
# Minimum
item from list with max value
minList
= [100,80,2070,8]
print min(minList)
>>>
8
List function : list(<sequence>) : Convert strings, tuple in to list.
#Convert
string into List
myString
= 'python'
myList =
list(myString) # Using list constructor function
converting string in to list
print myList
>>>
['p', 'y', 't', 'h', 'o', 'n']
#converting
tuple in to list
myTuple
= (1,2,3,4,(1,2,3)) #
Tuple within tuple-Here 1,2,3 is own tuple inside of other tuple
myTupleList
= list(myTuple) # Using list constructor function
converting tuple in to list. In this case myTuple is an argument to list
constructor function.
print myTupleList
>>>
[1, 2, 3, 4, (1, 2, 3)]
No comments:
Post a Comment