Python Everyday 09: get n largest or smallest nums

Table of Content

How can we get the n largest items from a list, you may write code like this:

It's will work, but we can use 'heapq' to write much shorter and more efficient code:

Similarly to "n largest", you can use 'heap.nsmallest(n, some_list)' to get the n smallest nums from a list.