# Experimenting around with this exercise

rating_out_of_10 = [1,2,3,4,5,6,7,8,9,10] for list in rating_out_of_10: if list >5: print(rating_out_of_10) print(rating_out_of_10[0]) rating_out_of_10.pop(0) else: print("This number" + str(rating_out_of_10[0]) +" is less than 5 so it has been removed") rating_out_of_10.pop(0)

Hello,
Just experimenting around with this exercise. I was wondering why the output seemed to stop at #3 so I plugged it into pythontutor. It showed me that the list function here seems to update by 1, 3, 6, etc in a cumulative fashion. Is this meant to be the case with intergers? I guess I need to change the list to strings.

Edit: I see this is a lack of understanding about what exactly loop variables are. It makes sense that it would be updating each time it cycles through the list. I tried the string solution but realised that wouldnâ€™t work with the list>5 aspect of things.

Edit 2: I can see where I went wrong now. The for loop runs whatever number is in position 0,1,2,3â€¦ but when I do the pop function. It removes a number but the loop will just continue on.

1 Like

Hi,
You have a few patterns that are causing some hard to control behaviour.
Generally, for control it is better to not change the size of the list as you iterate over it. This can be done by iterating the range of the length and maybe using a second array to deposit operations.

Then thereâ€™s the very sneaky `pop(0)` pattern that although itâ€™s legal python, runs very inefficiently. `pop()` works best when removing an item from the end of an array as you get it in constant time (the reason that pop(0) is worse is that all the data has to get shifted in memory by one block to adjust the new indexing, whereas removing the last item does not change the indices of the other data). If you want to remove the head of an array, consider using a queue to make it quicker (python has a `queue` library you can import from `collections`, itâ€™s fairly easy to pick up).

3 Likes

Thanks for the advice. Iâ€™ll have to look back over this post when my understanding has increased a little. This part stood out to me though as a reminder of why people frequently seem to use another list.

Iâ€™ll look up the queue.

In my mind, I guess my thought process went like thisâ€¦

â€śRemove the head and then have the loop run from the beginning againâ€¦?â€ť - but I guess thatâ€™s just silly to do.

rating_out_of_10 = [1,2,3,4,5,6,7,8,9,10 ] experiment_out_of_10 = [] for test in rating_out_of_10: if test > 5: experiment_out_of_10.append(test) print(experiment_out_of_10) #The problem is that when I do remove pop(0), the loop function aspect won't really 'reset' from the beginning' and restart itself. In which case, I just need completely different code. # Guess this is the reason why people tend to prefer to create a new list. else: print("The number " + str(test) +" is less than 5 so it has been removed")