Using List as Stack and Queues in Python
Prerequisite : list in Python
The concept of Stack and Queue is easy to implement in Python.
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:
# Python code to demonstrate Implementing # stack using list 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']
Implementing queue is a bit different. Queue works on the principle of “First-in, first-out”. Time plays an important factor here. We saw that during the implementation of stack we used append() and pop() function which was efficient and fast because we inserted and popped elements from the end of the list, but in queue when insertion and pops are made from the beginning of the list, it 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 collections.deque 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:
# Python code to demonstrate Implementing # Queue using deque and list 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'])
This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.
Recommended Posts:
- Python | Stack using Doubly Linked List
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Stack in Python
- numpy.stack() in Python
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert mixed data types tuple list to string list
- Python | Merge List with common elements in a List of Lists
- Python | Replace elements in second list with index of same element in first list
- Python | Convert list of string into sorted list of integer
- Python | Add list elements with a multi-list based on index
- Python | Pair and combine nested list to tuple list
- Python | Find maximum length sub-list in a nested list
- Python | Filter list of strings based on the substring list
- Python | Sorting list of lists with similar list elements