How to get the max value in a nested list?

Im trying to get the MAX price for every ‘country’ in this list.

my_list = [
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0),'0,012'], 
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0),'0,153'], 
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,114'],
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,109'],
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,252'],
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,012'], 
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,113'], 
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,116'],
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,250'],
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,266'],
    ['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,112'], 
    ['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,025'], 
    ['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,224'],
    ['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,256'],
    ['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,245']]

This is my code:

  max_price_per_list = []
  for lst in my_list:
      for price in lst:
          max_value = price[-1]
          max_price_per_list.append(max_value)
  print(max_price_per_list)

I received the following error: TypeError: 'datetime.datetime' object is not subscriptable

I expected the following outcome when I printend max_price_per_list:

['0,252', '0,266', '0,256']
   

Answer

Try this:

import datetime

my_list = [
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0),'0,012'], 
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0),'0,153'], 
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,114'],
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,109'],
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,252'],
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,012'], 
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,113'], 
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,116'],
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,250'],
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,266'],
    ['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,112'], 
    ['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,025'], 
    ['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,224'],
    ['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,256'],
    ['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,245']]

max_prices_by_country = {}

for item in my_list:
    price = max_prices_by_country.setdefault(item[0], 0)
    max_prices_by_country[item[0]] = max(price, float(item[-1].replace(',', '.')))

max_price_per_list = [str(price).replace('.', ',') for price in max_prices_by_country.values()]
print(max_price_per_list)