I'm attempting to create an insert Sort which takes the smallest number from a list and appends it to another list.
The problem is, everything I attempt to pop() the number out of the list, I get an index error.
Here's my code:
alist = [2,9,8,6,1]
blist =[]
def insertsort(list, slist) :
for item in list:
smallest = list[0]
if item < smallest:
smallest = list[item]
list.pop(smallest)
slist.append(smallest)
insertsort(alist, blist)
print(blist)
And the error is:
IndexError: pop index out of range
Thanks in advance for any help.
import heapqand uselist=heapq.heapify(list)followed by repeated calls toheapq.heappop(list), instead of looping throughlistto find the minimum by hand and usinglist.pop(). Theheapqmodule is part of the standard Python library and is built for exactly the purpose of efficiently keeping track of the smallest value in a sequence.