0

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.

Community
  • 1
  • 1
Gabriel A.
  • 109
  • 1
  • 2
  • 11

1 Answers1

1

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)
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • How would I go about doing this if there is a string at the front of the lists? So for example, scores = `[['bob',1,2,3,6],['joe',1,2,3,9]]` – Gabriel A. Feb 17 '16 at 14:00
  • Yeah, I tried to change the slice to `score_list[1:5]` in order to calculate the highest number for both of the lists but I get `TypeError: unorderable types: int() > str()` – Gabriel A. Feb 17 '16 at 14:06
  • @TeeKayM Then you want to slice before you take the max. Maybe `print (max(max(lst[1:5]) for lst in scores))` – miradulo Feb 17 '16 at 14:11