2

I have started learning python recently and came across one doubt related to lists.

Here is my example list .

>>>list1
[1, 2.3, 4, 'a', 300, 'b']
>>> max(list1)
'b'

Although 'b' carries an ascii value of 98 ,which is very much lesser than its neighboring element 300 ,python returns 'b' as MAX element.

So,anyone please let me know how do python calculates the maximum element?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
chetan honnavile
  • 398
  • 1
  • 6
  • 19

3 Answers3

2

Except for comparing numbers to numbers and string types to string types (which work as you'd expect), "weird" comparisons are done consistently, but arbitrarily. Some types also have predefined comparison functions which also overrides this rule: for example None always compares less than everything else.

See the language reference: https://docs.python.org/2/library/stdtypes.html#comparisons

Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result). Furthermore, some types (for example, file objects) support only a degenerate notion of comparison where any two objects of that type are unequal. Again, such objects are ordered arbitrarily but consistently. The <, <=, > and >= operators will raise a TypeError exception when any operand is a complex number.

If you're using CPython, it has a more strongly defined comparison:

CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
2

max will just compare the elements in the list normally — i.e.

>>> 'b' > 300
True

The reason for why this is true is in the Python documentation, and is also discussed in detail in this answer: How does Python compare string and int.

Notably, from the documentation,

Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily

in other words, when you try to compare non-numeric, non-string types, implementations are free to order them however they choose, as long as it's consistent. For CPython, which is almost definitely what is relevant to you:

Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

This behaviour can be demonstrated by making your own classes, e.g.

>>> class a(object):
...     pass
...
>>> class z(object):
...     pass
...
>>> class ZZZ(object):
...     pass
...
>>> A = a()
>>> Z = z()
>>> foo = ZZZ() # It's the *type* name that matters, not the variable name.
>>> type(A)
<class '__main__.a'>
>>> type(Z)
<class '__main__.z'>
>>> type(foo)
<class '__main__.ZZZ'>
>>> A > 'a'
False
>>> Z > 'a'
True
>>> max(A, foo, Z)
<class '__main__.ZZZ'>
Community
  • 1
  • 1
Kkelk
  • 136
  • 6
0

Integers and floats are compared according to numerical value. Otherwise, data types are compared by the name of the type in alphabetical order like so. And by the way, Python 3 doesn't allow this.

 1 < 2.3 < 4 < 300 < 'a' < 'b' # 300 (int) < 'a'(str)
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70