lundi 14 décembre 2020

How to get the second to last value in a list?

I have a list which contains multimple lists inside it and it looks like this:

my_list = [
    ['Morocco', 'Fish', '0,012'], 
    ['Morocco', 'Fish', '0,153'], 
    ['Morocco', 'Fish', '0,114'],
    ['Morocco', 'Fish', '0,109'],
    ['Morocco', 'Fish', '0,252'],
    ['Spain', 'Fish', '0,012'], 
    ['Spain', 'Fish', '0,113'], 
    ['Spain', 'Fish', '0,116'],
    ['Spain', 'Fish', '0,250'],
    ['Spain', 'Fish', '0,266'],
    ['Italy', 'Fish', '0,112'], 
    ['Italy', 'Fish', '0,025'], 
    ['Italy', 'Fish', '0,224'],
    ['Italy', 'Fish', '0,256'],
    ['Italy', 'Fish', '0,245']]

And I have a variable which looks like this:

my_limit = 0.2

Now for every list I want to get the last value before it exceeds my_limit. I want that value to be stored in a new variable.

This is what I tried:

for l in my_list:
    if row[-1]  #<-- this is actually where I block.... I dont know how to fix it.

This is the output I expect:

0,109
0,116
0,025

Aucun commentaire:

Enregistrer un commentaire