0

currently i am learning about stacks and queues. Tried to implement new knowledge, but can't understand why the popping doesn't remove all elements from my queue, only half of them. In this code i tried to do an example for queue, but i understand that with such pop method i created LIFO manner, which is a stack. So lets not take into account that, i am just trying to find an answer to "Why my for loop removes only half of the elements in my list".

Here is my code where i have empty queue or line with customers at the beginning. Then using for loop i add numbers from 1 to 10 inside the queue. Then using for loop i tried to remove all customers from the line and print out the customer iD when it is removed. For example, removed customer iD 10, 9, 8 etc.

However, it removes only half of the customers in the line and when i print queue after the for loop i still have 5 elements in it.

queue = [] # or a line of customers before they have arrived.
print("Empty line: ", queue)
for number in range(1, 11):
    queue.append(number) # here i add 10 customers to the line.
print("Line after 10 people have arrived: ", queue)
for number in queue:
    print("Removed customer iD: ", queue.pop()) # here i tried to remove all customers
print(queue)

Output:

Empty line:  []
Line after 10 people have arrived:  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Removed customer iD: 10
Removed customer iD: 9
Removed customer iD: 8
Removed customer iD: 7
Removed customer iD: 6
[1, 2, 3, 4, 5]
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69

1 Answers1

0

As a standard practice, you never want to modify an iterator you're looping over. Instead, use a separate iterator to loop over and then modify your queue array in the body of the loop:

queue = [] # or a line of customers before they have arrived.                         
print("Empty line: ", queue)                                                          
for number in range(1, 11):                                                           
    queue.append(number) # here i add 10 customers to the line.                       
print("Line after 10 people have arrived: ", queue)                                   
for i in range(0, len(queue)):                                                        
    print("Removed customer iD: ", queue.pop()) # here i tried to remove all customers

I'm looping over a range iterator and modifying the queue in the body of the for loop.

The reason you're seeing half of the elements left in the original queue is because the iterator is checking the current index against the length of the array at each pass, the index and length of the array both reach 5 which stops the for loop:

for index, num in enumerate(queue):
    queue.pop()
    print("index num", { "index":index, "num":num , "len": len(queue)})

Prints the following:

Empty line:  []
Line after 10 people have arrived:  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
index num {'index': 0, 'num': 1, 'len': 9}
index num {'index': 1, 'num': 2, 'len': 8}
index num {'index': 2, 'num': 3, 'len': 7}
index num {'index': 3, 'num': 4, 'len': 6}
index num {'index': 4, 'num': 5, 'len': 5} # The iterator will increment the index to 5.
[1, 2, 3, 4, 5]