27

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.

Tushar Patil
  • 1,419
  • 1
  • 18
  • 21
  • If only want to compare strings then you should remove 123 from your example else you should modify your question to mention that you specifically want to compare different data types – Ghos3t Sep 07 '20 at 23:14

6 Answers6

18

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:

  • comparison of objects of the same type acts as you'd expect.
  • comparison of objects of different type are ordered by their type name on python2.x and raise an error on python3.x (Unless a custom comparison operator is defined.)
  • old style classes break all the rules but they shouldn't be used anyway.

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.

Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • I am using python 2.7.3 version – Tushar Patil Dec 09 '13 at 04:41
  • @Tushar.PUCSD -- That explains why you didn't get a `TypeError` ;-). But, other python implementations are free to return `123` from your example. Which type comes first in the ordering is "arbitrary". – mgilson Dec 09 '13 at 04:43
  • @mgilson, the print statement is a Python2ism too – John La Rooy Dec 09 '13 at 04:44
  • To get the Python2 behaviour in Python3, you need something like `max(list1, key=lambda x:(type(x), x))` which is a good reason to avoid sorting those sorts of things – John La Rooy Dec 09 '13 at 04:45
  • 1
    @gnibbler -- Sure. But, answering this question without saying how the behavior has changed in python3.x would be a severe disservice to the python community! After all, we can't have people thinking that it's OK to continue to compare other types now can we? :) – mgilson Dec 09 '13 at 04:45
10

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.

3
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

David Buck
  • 3,752
  • 35
  • 31
  • 35
2

Results in case of decimal numbers may appear weird. So, I just want to mention these and save some time for others.

b=['79.68', '9.11', '5.75']
max(b)
--> '9.11'

This happens because it compares the ascii of corresponding characters in each string, which we can confirm by the following:

[tuple(ord(j) for j in i) for i in b]
--> [(55, 57, 46, 54, 56), (57, 46, 49, 49), (53, 46, 55, 53)]
Zircoz
  • 504
  • 3
  • 14
0

I believe you can also specify the key within the max method as explained here.

So, you can see that by default the result zara is based on the length :

max(list1, key=len) -> zara

Zach Valenta
  • 1,783
  • 1
  • 20
  • 35
0

Something I would like to add apart from the other answers is that in Python 3 the max for a list of strings gives the following output :-

l=["pl","cl pi"]
print(max(l))
#OUTPUT --> pl

This is the case due to the checking done by max in Python 3, max checks the first character of all the strings and finds the highest (in ASCII order) and returns that as output!