0

monthlyAVG = [["January",6,3],["Februrary",7,3],["March",10,4],["April",13,6],["May",17,9],["June",20,12],["July",22,14],["August",21,14],["September",19,12],["October",14,9],["November",10,6],["December",7,3]]

I have the above code as a set list of values. Is there a way to find the highest and lowest numerical values from the list? I'm new to python so any answers would be appreciated!

I'm hoping to output the highest value and lowest value along with the month name so that it looks something like this:

January 3
July 22
finalerock44
  • 65
  • 2
  • 11

2 Answers2

0
monthlyAVG = [["January",6,3],["Februrary",7,3],["March",10,4],["April",13,6],["May",17,9],["June",20,12],["July",22,14],["August",21,14],["September",19,12],["October",14,9],["November",10,6],["December",7,3]]
check_num_list = [i[1:] for i in monthlyAVG]
print(max(map(max, check_num_list)))

That will print "22" being the largest number in the list. I do a list comprehension when creating check_num_list by only grabbing all elements in each sublist that isn't in index 0 (so only numbers) then I use map to find all the largest numbers in each sublist and then pass max one more time against it to find the largest number of the remaining numbers in print(max(map(max, check_num_list)))

Treatybreaker
  • 776
  • 5
  • 9
0

You can search for them with a for loop through your array:

# Init values to first month
max_value = monthlyAVG[0][1]
min_value = monthlyAVG[0][2]
max_month = min_month = monthlyAVG[0][0]

# Check the other values in the array to find and update the searched
for month in monthlyAVG[1:]:
  if month[1] > max_value:
    max_value = month[1]
    max_month = month[0]

  if month[2] < min_value:
    min_value = month[1]
    min_month = month[0]

print('{} {}'.format(min_month, min_value))
print('{} {}'.format(max_month, max_value))
Juan Diego Lozano
  • 989
  • 2
  • 18
  • 30