1

When I try to call the max() function on a list which contains a combination of integers and strings, it always returns a string, in Python 2.x

For example,

#input
li = [2, 232322, 78, 'python', 77]
max(li)

#output
'python'

What seems to be the logic behind this? How does Python compare a string with an integer?

smci
  • 32,567
  • 20
  • 113
  • 146
Manas Chaturvedi
  • 5,210
  • 18
  • 52
  • 104

3 Answers3

7

In python2, strings and numbers compare in arbitrary but consistent order.

See 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

In python3, this raises a type error.

TypeError: unorderable types: str() > int()
Daenyth
  • 35,856
  • 13
  • 85
  • 124
  • How does Python 2.7 compare a string with an integer? I mean, is there a logic behind this or is it something that is just defined? – Manas Chaturvedi Jan 14 '16 at 17:29
  • @ManasChaturvedi It's arbitrary – Daenyth Jan 14 '16 at 17:30
  • 1
    @Daenyth - in cPython not totally arbitrary, a bit further down in the doc you linked: _**CPython implementation detail:** Objects of different types except numbers are ordered by their type names_ - so strings come before ints because `'str' > 'int'` – mata Jan 14 '16 at 17:47
  • @mata It's not arbitrary at runtime, the decision of how they are sorted when they can't be sorted is arbitrary - in this case it's str > int but that's not required - as implied by the bit you quoted saying `implementation detail` – Daenyth Jan 14 '16 at 17:48
  • It's not arbitrary in cPython: they are sorted first by type-name, then sort-order within that type. So `sorted( ['a',1,'1','c',2,0,'e','d',3,'b','2','0',9e99, 9e-99] ) = [0, 9e-99, 1, 2, 3, 9e+99, '0', '1', '2', 'a', 'b', 'c', 'd', 'e']`. Numeric first, then string. Looks very deterministic to me. (Does PyPy follow this?) – smci Jan 14 '16 at 18:48
  • @smci I never said it wasn't deterministic - deciding that types sort by type name is arbitrary. – Daenyth Jan 14 '16 at 19:01
1

The comparision between strings and numbers is undefined. It depends on the version of python. Strings are either always larger than any number or lower.

The manual states, that in CPython different objects are compared after their type name, if no other comparision is defined.

Daniel
  • 42,087
  • 4
  • 55
  • 81
0

In Python 2.x The max() and min() functions first internally sort the list, resulting in a list similar to the output of list.sort() and then outputs the last (max) and first(min) item of the list-

>>> li = [2, 232322, 78, 'python', 77]
>>> max(li)
'python'
>>> min(li)
2

As you can see below if you sort the list the first and last item in the list are the result produce by min and max functions

>>> li = [2, 232322, 78, 'python', 77]
>>> li.sort()
>>> print li
[2, 77, 78, 232322, 'python']
S G
  • 19
  • 4
  • Are you sure it "first internally sort[s] the list"? This would be very inefficient, and looking at the source code it seems to just do the expected sequential traversal looking for the smallest or largest entry. – Ken Y-N Jan 19 '18 at 02:49