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]