<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Finding the Largest or Smallest N Items
Problem
You want to make a list of the largest or smallest N items in a collection.
Solution
The heapq module has two functions—nlargest() and nsmallest()
Example 1
Step1:  Example 2
Use with key parameter
Step2:  Understand heap
heap converts data into an ordered list
Step3:  heap[0] is always the smallest item
Step4:  Sorted
You can also use sorted() function
<ASSISTANT_TASK:>
Python Code:
import heapq
nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] 
print(heapq.nlargest(3, nums)) # Prints [42, 37, 23] 
print(heapq.nsmallest(3, nums)) # Prints [-4, 1, 2]
Explanation: Finding the Largest or Smallest N Items
Problem
You want to make a list of the largest or smallest N items in a collection.
Solution
The heapq module has two functions—nlargest() and nsmallest()
Example 1
End of explanation
portfolio = [
       {'name': 'IBM', 'shares': 100, 'price': 91.1},
       {'name': 'AAPL', 'shares': 50, 'price': 543.22},
       {'name': 'FB', 'shares': 200, 'price': 21.09},
       {'name': 'HPQ', 'shares': 35, 'price': 31.75},
       {'name': 'YHOO', 'shares': 45, 'price': 16.35},
       {'name': 'ACME', 'shares': 75, 'price': 115.65}
]
cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
print(cheap)
print(expensive)
Explanation: Example 2
Use with key parameter
End of explanation
nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
heap = list(nums)
heapq.heapify(heap)
heap
Explanation: Understand heap
heap converts data into an ordered list
End of explanation
print(heapq.heappop(heap))
print(heapq.heappop(heap))
print(heapq.heappop(heap))
Explanation: heap[0] is always the smallest item
End of explanation
sorted(nums)[:3]
Explanation: Sorted
You can also use sorted() function
End of explanation
<END_TASK>