How to filter a list based on multiple other lists in Python?

Viewed 73

I have 3 lists:

minimal_values = ['0,32', '0,35', '0,45']
maximal_values = ['0,78', '0,85', '0,72']

my_list = [
    ['Morocco', 'Meat', '190,00', '0,15'], 
    ['Morocco', 'Meat', '189,90', '0,32'], 
    ['Morocco', 'Meat', '189,38', '0,44'],
    ['Morocco', 'Meat', '188,94', '0,60'],
    ['Morocco', 'Meat', '188,49', '0,78'],
    ['Morocco', 'Meat', '187,99', '0,101'],
    ['Spain', 'Meat', '190,76', '0,10'], 
    ['Spain', 'Meat', '190,16', '0,20'], 
    ['Spain', 'Meat', '189,56', '0,35'],
    ['Spain', 'Meat', '189,01', '0,40'],
    ['Spain', 'Meat', '188,13', '0,75'],
    ['Spain', 'Meat', '187,95', '0,85'],
    ['Italy', 'Meat', '190,20', '0,11'],
    ['Italy', 'Meat', '190,10', '0,31'], 
    ['Italy', 'Meat', '189,32', '0,45'],
    ['Italy', 'Meat', '188,61', '0,67'],
    ['Italy', 'Meat', '188,01', '0,72'],
    ['Italy', 'Meat', '187,36', '0,80']]

I want to filter my_list based on index [-1] and the values in minimal_values and maximal_values. So like this:

  1. For Morocco I only want the rows where index[-1] is between 0,32 and 0,78
  2. For Spain I only want the rows where index[-1] is between 0,35 and 0,85
  3. For Italy I only want the rows where index[-1] is between 0,45 and 0,72

I ultimately want my_list to look like this:

my_list = [
    ['Morocco', 'Meat', '189,90', '0,32'], 
    ['Morocco', 'Meat', '189,38', '0,44'],
    ['Morocco', 'Meat', '188,94', '0,60'],
    ['Morocco', 'Meat', '188,49', '0,78'],
    ['Spain', 'Meat', '189,56', '0,35'],
    ['Spain', 'Meat', '189,01', '0,40'],
    ['Spain', 'Meat', '188,13', '0,75'],
    ['Spain', 'Meat', '187,95', '0,85'],
    ['Italy', 'Meat', '189,32', '0,45'],
    ['Italy', 'Meat', '188,61', '0,67'],
    ['Italy', 'Meat', '188,01', '0,72']]

This is the code I tried:

for l in my_list:
    if l[-1] >= [x for x in minimal_values] and <= [x for x in maximal_values]:
        print(l)

I received the following output:

SyntaxError: invalid syntax
3 Answers

You would better get the country names in a separate list first. See below:

countries=[]
for i in my_list:
    if i[0] not in countries:
        countries.append(i[0])

#['Morocco', 'Spain', 'Italy']

Now save min and max values for each country in a dictionary:

d={countries[i]:(float(minimal_values[i].replace(',','.')), float(maximal_values[i].replace(',','.'))) for i in range(len(countries))}

#{'Morocco': (0.32, 0.78), 'Spain': (0.35, 0.85), 'Italy': (0.45, 0.72)}

And now do the filtering, as below:

result=[]

for i in my_list:
    if float(i[-1].replace(',','.'))>=d[i[0]][0] and float(i[-1].replace(',','.'))<=d[i[0]][1]:
        result.append(i) 

Full code and Output:

countries=[]
for i in my_list:
    if i[0] not in countries:
        countries.append(i[0])

d={countries[i]:(float(minimal_values[i].replace(',','.')), float(maximal_values[i].replace(',','.'))) for i in range(len(countries))}

result=[]

for i in my_list:
    if float(i[-1].replace(',','.'))>=d[i[0]][0] and float(i[-1].replace(',','.'))<=d[i[0]][1]:
        result.append(i) 

>>> print(result)

[['Morocco', 'Meat', '189,90', '0,32'], 
 ['Morocco', 'Meat', '189,38', '0,44'], 
 ['Morocco', 'Meat', '188,94', '0,60'], 
 ['Morocco', 'Meat', '188,49', '0,78'], 
 ['Spain', 'Meat', '189,56', '0,35'], 
 ['Spain', 'Meat', '189,01', '0,40'], 
 ['Spain', 'Meat', '188,13', '0,75'], 
 ['Spain', 'Meat', '187,95', '0,85'], 
 ['Italy', 'Meat', '189,32', '0,45'], 
 ['Italy', 'Meat', '188,61', '0,67'], 
 ['Italy', 'Meat', '188,01', '0,72']]

You could go for something like this:

minimal_values = ['0,32', '0,35', '0,45']
maximal_values = ['0,78', '0,85', '0,72']

my_list = [
    ['Morocco', 'Meat', '190,00', '0,15'], 
    ['Morocco', 'Meat', '189,90', '0,32'], 
    ['Morocco', 'Meat', '189,38', '0,44'],
    ['Morocco', 'Meat', '188,94', '0,60'],
    ['Morocco', 'Meat', '188,49', '0,78'],
    ['Morocco', 'Meat', '187,99', '0,101'],
    ['Spain', 'Meat', '190,76', '0,10'], 
    ['Spain', 'Meat', '190,16', '0,20'], 
    ['Spain', 'Meat', '189,56', '0,35'],
    ['Spain', 'Meat', '189,01', '0,40'],
    ['Spain', 'Meat', '188,13', '0,75'],
    ['Spain', 'Meat', '187,95', '0,85'],
    ['Italy', 'Meat', '190,20', '0,11'],
    ['Italy', 'Meat', '190,10', '0,31'], 
    ['Italy', 'Meat', '189,32', '0,45'],
    ['Italy', 'Meat', '188,61', '0,67'],
    ['Italy', 'Meat', '188,01', '0,72'],
    ['Italy', 'Meat', '187,36', '0,80']]
    

# Convert values to float.
minimal_values = [float(i.replace(',', '.')) for i in minimal_values]
maximal_values = [float(i.replace(',', '.')) for i in maximal_values]

# Collect all unique countries in a list.
countries = list(set(country[0] for country in my_list))

results = []
for l in my_list:
    i = countries.index(l[0])
    if minimal_values[i] <= float(l[-1].replace(',', '.')) <= maximal_values[i]:
        results.append(l)
 print(results)

Output:

[['Morocco', 'Meat', '189,90', '0,32'],
['Morocco', 'Meat', '189,38', '0,44'],
['Morocco', 'Meat', '188,94', '0,60'],
['Morocco', 'Meat', '188,49', '0,78'],
['Spain', 'Meat', '189,56', '0,35'],
['Spain', 'Meat', '189,01', '0,40'],
['Spain', 'Meat', '188,13', '0,75'],
['Spain', 'Meat', '187,95', '0,85'],
['Italy', 'Meat', '189,32', '0,45'],
['Italy', 'Meat', '188,61', '0,67'],
['Italy', 'Meat', '188,01', '0,72']]

First so you need to mentaion the item every time you will compare it with something else. If you want to sort them by comparing them with the minimal value and the maximal value so it will be better to create a new list for the new values.

minimal_values = ['0,32', '0,35', '0,45']
maximal_values = ['0,78', '0,85', '0,72']

my_list = [
    ['Morocco', 'Meat', '190,00', '0,15'],
    ['Morocco', 'Meat', '189,90', '0,32'],
    ['Morocco', 'Meat', '189,38', '0,44'],
    ['Morocco', 'Meat', '188,94', '0,60'],
    ['Morocco', 'Meat', '188,49', '0,78'],
    ['Morocco', 'Meat', '187,99', '0,101'],
    ['Spain', 'Meat', '190,76', '0,10'],
    ['Spain', 'Meat', '190,16', '0,20'],
    ['Spain', 'Meat', '189,56', '0,35'],
    ['Spain', 'Meat', '189,01', '0,40'],
    ['Spain', 'Meat', '188,13', '0,75'],
    ['Spain', 'Meat', '187,95', '0,85'],
    ['Italy', 'Meat', '190,20', '0,11'],
    ['Italy', 'Meat', '190,10', '0,31'],
    ['Italy', 'Meat', '189,32', '0,45'],
    ['Italy', 'Meat', '188,61', '0,67'],
    ['Italy', 'Meat', '188,01', '0,72'],
    ['Italy', 'Meat', '187,36', '0,80']]

my_list2 = []
for country in my_list:
    if country[0] == "Morocco" and country[-1] >= minimal_values[0] and country[-1] <= maximal_values[0]:
        my_list2.append(country)

    if country[0] == "Spain" and country[-1] >= minimal_values[1] and country[-1] <= maximal_values[1]:
        my_list2.append(country)

    if country[0] == "Italy" and country[-1] >= minimal_values[2] and country[-1] <= maximal_values[2]:
        my_list2.append(country)

print(my_list2)

As you can see so I've mentioned the last value of each list which has the index -1 every time I wanted to comapre them. It was possible to compare strings, but it will be better if you set the values to float or integers next time. That will make the process easier.

The output that you will get is:

['Morocco', 'Meat', '189,90', '0,32'], 
    ['Morocco', 'Meat', '189,38', '0,44'],
    ['Morocco', 'Meat', '188,94', '0,60'],
    ['Morocco', 'Meat', '188,49', '0,78'],
    ['Spain', 'Meat', '189,56', '0,35'],
    ['Spain', 'Meat', '189,01', '0,40'],
    ['Spain', 'Meat', '188,13', '0,75'],
    ['Spain', 'Meat', '187,95', '0,85'],
    ['Italy', 'Meat', '189,32', '0,45'],
    ['Italy', 'Meat', '188,61', '0,67'],
    ['Italy', 'Meat', '188,01', '0,72']]
Related