i have a python list whose items are objects of a class with various attributes such as birthday_score, anniversary_score , baby_score..... I want to to sort the list on the basis of one of these attributes ,say anniversary_score. How do i do it ?
your_list.sort(key = lambda x : x.anniversary_score)
or if the attribute name is a string then you can use :
import operator
your_list.sort(key=operator.attrgetter('anniversary_score'))
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