# find the n smallest and greatest numbers in list
import heapq
numbers = [10, 40, 25, 500, 90, 59, 320, 200, 100, 800]
print(heapq.nlargest(4, numbers))
# [800, 500, 320, 200]
import heapq
numbers = [10, 40, 25, 500, 90, 59, 320, 200, 100, 800]
print(heapq.nsmallest(4, numbers))
# [10, 25, 40, 59]