However, with Python 3, you can pass in the keyword argument default with a value that will be returned if the sequence is empty, instead of raising an exception:
Extracting N largest or N smallest items from an iterable
To find some number (more than one) of largest or smallest values of an iterable, you can use the nlargest and nsmallest of the heapq module:
This is much more efficient than sorting the whole iterable and then slicing from the end or beginning. Internally these functions use the binary heappriority queue data structure, which is very efficient for this use case.
Like min, max and sorted, these functions accept the optional key keyword argument, which must be a function that, given an element, returns its sort key.
Here is a program that extracts 1000 longest lines from a file:
Here we open the file, and pass the file handle f to nlargest. Iterating the file yields each line of the file as a separate string; nlargest then passes each element (or line) is passed to the function len to determine its sort key. len, given a string, returns the length of the line in characters.
This only needs storage for a list of 1000 largest lines so far, which can be contrasted with
which will have to hold the entire file in memory.
Getting a sorted sequence
Using one sequence:
The result is always a new list; the original data remains unchanged.
Getting the minimum or maximum of several values
Make custom classes orderable
min, max, and sorted all need the objects to be orderable. To be properly orderable, the class needs to define all of the 6 methods __lt__, __gt__, __ge__, __le__, __ne__ and __eq__:
functools.total_ordering decorator can be used simplifying the effort of writing these rich comparison methods. If you decorate your class with total_ordering, you need to implement __eq__, __ne__ and only one of the __lt__, __le__, __ge__ or __gt__, and the decorator will fill in the rest:
Notice how the > (greater than) now ends up calling the less than method, and in some cases even the __eq__ method. This also means that if speed is of great importance, you should implement each rich comparison method yourself.
Minimum and Maximum of a sequence
Getting the minimum of a sequence (iterable) is equivalent of accessing the first element of a sorted sequence:
The maximum is a bit more complicated, because sorted keeps order and max returns the first encountered value. In case there are no duplicates the maximum is the same as the last element of the sorted return:
But not if there are multiple elements that are evaluated as having the maximum value:
Any iterable containing elements that support < or > operations are allowed.
Special case: dictionaries
Getting the minimum or maximum or using sorted depends on iterations over the object. In the case of dict, the iteration is only over the keys:
To keep the dictionary structure, you have to iterate over the .items():
For sorted, you could create an OrderedDict to keep the sorting while having a dict-like structure:
Again this is possible using the key argument:
Using the key argument
Finding the minimum/maximum of a sequence of sequences is possible:
but if you want to sort by a specific element in each sequence use the key-argument:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0