Let's say I have a list of items I want to sort: items = [ item1, item2, item3 ]
. The attribute I want to use to sort them is item.data.value
, so I'd normally go:
sorted(items, key=attrgetter('data.value'))
And that'd work just fine. However, data
can actually be None
so obviously I couldn't access value
.
How do you usually deal with scenarios like this?
PS: neither this question nor this one helped.
key is a function that generates an intermediate value for each element, and this value is used to do the comparisons during the sorting process. The sort() method mutates the list, causing permanent changes. You need to be very careful and only use it if you do not need the original version of the list.
sort() Syntax The syntax of the sort() method is: list.sort(key=..., reverse=...) Alternatively, you can also use Python's built-in sorted() function for the same purpose.
Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted list from an iterable.
To sort a dictionary by value in Python you can use the sorted() function. Python's sorted() function can be used to sort dictionaries by key, which allows for a custom sorting method. sorted() takes three arguments: object, key, and reverse .
sorted(items, key=lambda i: i.data.value if i.data else 0)
Use as key a tuple, like (False, value)
. If value is None, then the tuple should be (True, None)
.
Tuples are compared by their first element first, then the second, et cetera. False sorts before True. So all None values will be sorted to the end.
def none_to_end_key(item):
value = item.data.value if item.data else None
return (value is None, value)
sorted(items, key=none_to_end_key)
Will sort all None values to the end.
I see now that you have tagged your question Python-2.7, then this is probably overkill. In Python 3, comparing None to an integer or string raises an exception, so you can't simply sort a list with None and other values, and something like this is needed.
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