import heapq nums = [1, 2, 3, 4, 5, 6] print(heapq.nlargest(3, nums)) # [6, 5, 4] print(sorted(nums, reverse=True)[:3]) # [6, 5, 4] print(heapq.nsmallest(3, nums)) # [1, 2, 3]