Prerequisites : list and Deque in Python.
Unlike C++ STL and Java Collections, Python does have specific classes/interfaces for Stack and Queue. Following are different ways to implement in Python
1) Using list
Stack works on the principle of “Last-in, first-out”. Also, the inbuilt functions in Python make the code short and simple. To add an item to the top of the list, i.e., to push an item, we use append() function and to pop out an element we use pop() function. These functions work quiet efficiently and fast in end operations.
Let’s look at an example and try to understand the working of push() and pop() function:
Example:
stack = [ "Amar" , "Akbar" , "Anthony" ]
stack.append( "Ram" )
stack.append( "Iqbal" )
print (stack)
print (stack.pop())
print (stack)
print (stack.pop())
print (stack)
|
Output:
['Amar', 'Akbar', 'Anthony', 'Ram', 'Iqbal']
Iqbal
['Amar', 'Akbar', 'Anthony', 'Ram']
Ram
['Amar', 'Akbar', 'Anthony']
Queue works on the principle of “First-in, first-out”. Below is list implementation of queue. We use pop(0) to remove the first item from a list.
queue = [ "Amar" , "Akbar" , "Anthony" ]
queue.append( "Ram" )
queue.append( "Iqbal" )
print (queue)
print (queue.pop( 0 ))
print (queue)
print (queue.pop( 0 ))
print (queue)
|
Output:
['Amar', 'Akbar', 'Anthony', 'Ram', 'Iqbal']
Amar
['Akbar', 'Anthony', 'Ram', 'Iqbal']
Akbar
['Anthony', 'Ram', 'Iqbal']
2) Using Deque
In case of stack, list implementation works fine and provides both append() and pop() in O(1) time. When we use deque implementation, we get same time complexity.
from collections import deque
queue = deque([ "Ram" , "Tarun" , "Asif" , "John" ])
print (queue)
queue.append( "Akbar" )
print (queue)
queue.append( "Birbal" )
print (queue)
print (queue.pop())
print (queue.pop())
print (queue)
|
Output:
deque(['Ram', 'Tarun', 'Asif', 'John'])
deque(['Ram', 'Tarun', 'Asif', 'John', 'Akbar'])
deque(['Ram', 'Tarun', 'Asif', 'John', 'Akbar', 'Birbal'])
Birbal
Akbar
deque(['Ram', 'Tarun', 'Asif', 'John'])
But when it comes to queue, the above list implementation is not efficient. In queue when pop() is made from the beginning of the list which is slow. This occurs due to the properties of list, which is fast at the end operations but slow at the beginning operations, as all other elements have to be shifted one by one.
So, we prefer the use of dequeue over list, which was specially designed to have fast appends and pops from both the front and back end.
Let’s look at an example and try to understand queue using collections.deque:
from collections import deque
queue = deque([ "Ram" , "Tarun" , "Asif" , "John" ])
print (queue)
queue.append( "Akbar" )
print (queue)
queue.append( "Birbal" )
print (queue)
print (queue.popleft())
print (queue.popleft())
print (queue)
|
Output:
deque(['Ram', 'Tarun', 'Asif', 'John'])
deque(['Ram', 'Tarun', 'Asif', 'John', 'Akbar'])
deque(['Ram', 'Tarun', 'Asif', 'John', 'Akbar', 'Birbal'])
Ram
Tarun
deque(['Asif', 'John', 'Akbar', 'Birbal'])
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 May, 2022
Like Article
Save Article