Reputation: 35
>>> max([1.0,1])
1.0
>>> max([1,1.0])
1
>>> 1==1.0
True
Why is max()
returning the first value that the list has?
Upvotes: 0
Views: 471
Reputation: 182
According to Python's official documentation of the max() function:
If multiple items are maximal, the function returns the first one encountered.
Since 1.0 == 1, whichever comes first between 1.0 and 1 is returned by the function.
Upvotes: 8
Reputation: 2611
What you describe is how the function is specified to work:
If multiple items are maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as
sorted(iterable, key=keyfunc, reverse=True)[0]
andheapq.nlargest(1, iterable, key=keyfunc)
.
Upvotes: 2