Sai Krishna Maddali
Sai Krishna Maddali

Reputation: 35

Why is max([1.0,1]) 1.0 and max([1,1.0]) 1?

>>> 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

Answers (2)

Rohit Joshi
Rohit Joshi

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

jerry
jerry

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] and heapq.nlargest(1, iterable, key=keyfunc).

Upvotes: 2

Related Questions