listmathinequalities

How can i sort a list from specifik criteria


I have this list and I want to sort the list. This is just a smaller example of what I want to do, but I get the same error. I dont understand why I can't make this work. I have tried using google to solve the problem but without luck.

lst = [3, 4, 5, 6]

if lst < 4:
    lst.pop()
    print(lst)

How can i do this it shows

TypeError:'<' not supported between instances of 'list' and 'in


Solution

  • I think that your goal is to remove all elements in the list that are lesser than 4. You can use this simple list comprehension in order to achieve what you want:

    lst = [3, 4, 5, 6]
    lst = [elem for elem in lst if elem >= 4]
    print(lst)
    

    Output:

    [4, 5, 6]