Python ProgrammingPython Programming

How to get N Largest and Smallest list of elements from a list?

import heapq

thelist = [1, 4, 2, 3, 5, 4, 5, 6, 7, 8, 1, 3, 4, 5, 9, 10, 11]

print(heapq.nlargest(5, thelist))
print(heapq.nsmallest(5, thelist))
Output
[11, 10, 9, 8, 7]
[1, 1, 2, 3, 3]