I have implemented a lambda function to sort a list. now i want to remove all the negative objects from the list by using lambda functions .
dto_list.sort(key=lambda x: x.count, reverse=True)
any one know a way to write the lambda expression to do it? I could not find a proper tutorial
Not very pythonic but here is how to do it with a lambda
>>> L = list(range(-10,10))
>>> L
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> filter(lambda x: x >= 0, L)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
Most people would use a list comprehension
>>> [x for x in L if x >= 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With