i want to print highest number from my list (1231 here). i tried doing this here(posted below), but that doesn't work. it just shows the highest total number for each element in my list, which is, obviously, not what I want.
list = [ [1,3,251], [2], [1231,52,22] ]
for i in range(len(list)):
for j in range(len(list[i])):
print(max(list))
print:
[1231, 52, 22]
[1231, 52, 22]
[1231, 52, 22]
[1231, 52, 22]
[1231, 52, 22]
[1231, 52, 22]
[1231, 52, 22]
CodePudding user response:
Something like this:
xss = [ [1,3,251], [2], [1231,52,22] ]
result = max(max(xs) for xs in xss)
print(result)
CodePudding user response:
You are not using i
and j
anywhere in your code. Since you have a nested list you should have nested max
calls:
>>> my_list = [[1, 3, 251], [2], [1231, 52, 22]]
>>> max(max(sub_list) for sub_list in my_list)
1231
Alternatively you can use a nested list comprehension to flatten the list, then make a single max
call:
>>> max(i for sub_list in my_list for i in sub_list)
1231
CodePudding user response:
list = [ [1,3,251], [2], [1231,52,22] ]
for i in list:
print(max(i))
You don't have to go through each list from the bigger list since you're using max
.
CodePudding user response:
>>> list = [ [1,3,251], [2], [1231,52,22] ]
>>> max(max(list))
1231
It takes de max from the first list thant output the max from that list.