Join Regular Classroom : Visit ClassroomTech

Pop first item from queue in Python

pop method – list.pop()

Also Read: Remove first value of array in Python

Example:

# code to understand how to pop first item from queue in Python
# www.codewindow.in

a = []

# Let's enqueue some fruits into our list
a.append('code')
a.append('window')
a.append('IT')
a.append('web')

# Now let's dequeue our fruits, we should get 'code'
item = a.pop(0)
print(item)

# If we dequeue again we'll get 'window'
item = a.pop(0)
print(item)

# 'IT' and 'web' remain
print(a)

Output:

code
window
['IT', 'web']

Explanation:
Here, pop(0) keeps in popping the first element from the updated list and the popped item is stored in item.


Follow Us

You Missed

Also Checkout