I have a list:
list1 = [123, 'xyz', 'zara', 'abc']
print "Max value element : ", max(list1);
It gives:
Max value element : zara
Please also explain how it perform comparison on list of strings and list of numbers.
This is actually a good question and the answer varies depending on whether you're on python2.x or python3.x ... And which python implementation you're using1.
See here for a description of how python compares different types. The link says pretty much all that you need to know, but as a quick summary:
1Hopefully you can see by the amount of uncertainty there that this is not really well defined and so it's a situation that you should try to avoid.
It "orders" the words alphabetically and returns the one that is at the bottom of the alphabetic list (for the record, it doesn't not change the order of the items in your list, that's why I wrote "orders" inside quotation marks):
list1 = ["kyle", "darius"]
max(list1)
--> returns kyle because k is after d
list2 = ["kaula", "kzla", "kayla", "kwala"]
max(list2)
--> returns kzla because kz is alphabetically ordered after ka and kw
list3 = ["kyle", "darius", "janna", "set", "annie", "warwick", "bauuuuuu"]
max(list3)
--> returns warwick
I'm using python 3.7, and when I try to mix strings with numbers:
list4 = [13341412, "zara", "jane", "kada"]
max(list4)
I get an error:
Traceback (most recent call last): File "", line 1, in TypeError: '>' not supported between instances of 'str' and 'int'
At least in python 3.7, you cannot mix integers with strings.
list1=['2020','4','890','70','891','898']
max(list1)
--> returns 898
in case of only numbers enclosed as string, it will compare the first maximum digit, if there are two(in this case: three which start with 8)-
it will look for second digit of same number and continue to compare till it find the bigger one. hence return 898
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With