How to sort and filter a list in Python?

Question:

I have a list of list in Python that I want to sort based on two categories. The sorted list should have the lowest number in the last position and also the first element has to be greater than 400. I want to return the top 3 from the list.

x_list = [['422', '324', '733443'], ['342', '654', '674335'], 
         ['953', '456', '828854'], ['345', '886', '446678'], 
         ['3224', '533', '654333'], ['7545', '5567', '369990']]

It should return like this:

result = [['7545', '5567', '369990'], ['3224', '533', '654333'], ['422', '324', '733443']]

I have tried using the following code, but cannot combine the two conditions to sort the list:

result= []
result += sorted(x_list, key=lambda x: int(x[-1]))[:3]

Can anyone plz help me to solve this? Thank you very much!!

Asked By: user19239587

||

Answers:

I would first filter the list to only retain values where first element is greater than 400, and then sort the final result by ascending order based on last element, as you originally had it. For example:

x_list = [['422', '324', '733443'], ['342', '654', '674335'],
         ['953', '456', '828854'], ['345', '886', '446678'],
         ['3224', '533', '654333'], ['7545', '5567', '369990']]

x_list2 = [x for x in x_list if int(x[0]) > 400]
x_list2.sort(key=lambda x: int(x[2]))
x_list2 = x_list2[:3]

print(x_list2)
# [['7545', '5567', '369990'], ['3224', '533', '654333'], ['422', '324', '733443']]

Or you can also sort and filter in a single step as well:

x_list = [['422', '324', '733443'], ['342', '654', '674335'],
         ['953', '456', '828854'], ['345', '886', '446678'],
         ['3224', '533', '654333'], ['7545', '5567', '369990']]

x_list2 = sorted((x for x in x_list if int(x[0]) > 400), key=lambda x: int(x[2]))[:3]

print(x_list2)
# [['7545', '5567', '369990'], ['3224', '533', '654333'], ['422', '324', '733443']]
Answered By: rv.kvetch
x_list = [['422', '324', '733443'], ['342', '654', '674335'],
         ['953', '456', '828854'], ['345', '886', '446678'],
         ['3224', '533', '654333'], ['7545', '5567', '369990']]

x_list = [a for a in x_list if float(a[0]) > 400]  # Filters out values <= 400
x_list.sort(key=lambda x : x[2])  # Sorts by the last element
x_list = x_list[:3]  # Only keeps the first 3 values
print(x_list)
Answered By: Telan

Consider this code:

filter_first = [el for el in x_list if int(el[0]) > 400]
sorted(filter_first, key=lambda el: el[-1])

This gives me

[['7545', '5567', '369990'], ['3224', '533', '654333'], ['422', '324', '733443'], ['953', '456', '828854']]

which is different from what you claimed you wanted the output to be, but I think it meets all your stated requirements. Can you explain the ‘953’ sublist?

Answered By: kojiro

Here’s a customizable function:

def sort_filtered(list_, key=lambda x: x[2], filter_=lambda x: x[0] > 400, n=3):   
    # Turn values into floats so we can sort them
    float_list = [[float(x) for x in y] for y in list_]
    return [x for x in sorted(float_list, key=key) if filter_][:n]
Answered By: 965311532
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.