Gabriel A.
Gabriel A.

Reputation: 129

Max() returns highest list, not number

I have a sample piece of code here:

scores = [[1,2,3,6],[1,2,3,9]]

highest = (max(scores[1:3]))
print (highest)

I am trying to print the highest number for both of the lists from index 1-3, but it just prints the highest list instead.

[1, 2, 3, 9]

Before people mark this as duplicate, I have searched other questions (with this one being the closest related) but none of them seem to work. Maybe I am missing a small key element.

Upvotes: 0

Views: 58

Answers (1)

Reut Sharabani
Reut Sharabani

Reputation: 31339

Try applying max to each list as well.

For max of max in each list:

print max(max(lst) for lst in scores[1:3])

For max per list:

print tuple(max(lst) for lst in scores[1:3])

Not that indexing starts from 0 so you'll get (9,). To get both:

print tuple(max(lst) for lst in scores[0:3])

Examples (python 3, so print is a function, not a statement):

>>> print(max(max(lst) for lst in scores[1:3]))
9
>>> print(tuple(max(lst) for lst in scores[1:3]))
(9,)
>>> print(tuple(max(lst) for lst in scores[0:3]))
(6, 9)

Upvotes: 1

Related Questions