I have the following list.
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']]
With below code im grabbing the highest value per country. That works correct, however. I would also like to add the country to max_price_per_list
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(',', '.')))
country = item[0]
max_price_per_list = [[country] + [str(price).replace('.', ',') for price in max_prices_by_country.values()]]
print(max_price_per_list)
This is my current output:
[['Spain', '0,252', '0,266']]
This is my preferred output:
[[Morocco, '0,252'],
['Spain', '0,266']]