How do I return a list of the 3 lowest values in another list. For example I want to get the 3 lowest values of this list:
in_list = [1, 2, 3, 4, 5, 6]
inpu
If your lists are long, the most efficient way of doing this is via numpy.partition:
>>> def lowest(a, n): return numpy.partition(a, n-1)[:n]
>>> in_list = [6, 4, 3, 2, 5, 1]
>>> lowest(in_list, 3)
array([1, 2, 3])
This executes in O(N) time, unlike a full sort which would operate in O(NlogN) time. The time savings come from not performing a full sort, but only the minimal amount needed to ensure that the n
lowest elements are first. Hence the output is not necessarily sorted.
If you need them to be sorted, you can do that afterwards (numpy.sort(lowest(in_list,3)) => array([1,2,3])
). For a large array this will still be faster than sorting the whole thing first.
Edit: Here is a comparison of the speed of numpy.partition
, heapq.nsmallest
and sorted
:
>>> a = numpy.random.permutation(np.arange(1000000))
>>> timeit numpy.partition(a, 2)[:3]
100 loops, best of 3: 3.32 ms per loop
>>> timeit heapq.nsmallest(3,a)
1 loops, best of 3: 220 ms per loop
>>> timeit sorted(a)[:3]
1 loops, best of 3: 1.18 s per loop
So numpy.partition
is 66 times faster than heapq.nsmallest
for an array with a million elements, and it is 355 times faster than sorted
. This doesn't mean that you should never use heapq.nsmallest
(which is very flexible), but it demonstrates how important it is to avoid plain lists when speed is important.