I'm currently a novice with Python and I'm trying to learn it efficiently. A project I created for myself requires me to take the minimum value of a list, then add it to another list, and then finally remove that minimum value and redo that process until there are no more values in the original list.
For example, if I have
list = [0, 3, 2, 1, 4, 2, 4, 5, 5]
I want to retrieve a list with the values:
list2 = [0, 1, 2, 2, 3, 4, 4, 5, 5] #(in this specific order)
list = [] # empty because values have been deleted.
Here's the code I already have:
count = 0
counter = len(group0)
while counter > 1:
while count < len(group0):
if min(group0) == group0[count]:
finalgroup0.append(group0[count]) #finalgroup0 is an empty list at the start.
group0.remove(group0[count]) #group0 has all of the values.
count += 1
else:
count += 1
counter -= 1
Note: The only reason I am deleting the value in list is so that I can take the min of the whole list once again. If this isn't necessary, please enlighten me on it. Also, this code worked to a certain extent, but it did not finish through the whole entire list. This is why I added the while loop outside so that once it reaches one it would be done, but that did not work either. I believe this is because it is checking all values for 'count' and checking if it is a min, and that is why the counter value needs to be higher. However, if I increase the counter value, then there is a 'list index, out of range' error.
I understand that my code is not the most efficient, but I've been at this and I have tried using a for loop and others but none of them have worked. If someone could please help me out, I would be greatly appreciative.
Thanks in advance.