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:
['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.
['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.
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 queue 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:
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 email@example.com. 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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Stack and Queue in Python using queue Module
- Python - Stack and StackSwitcher in GTK+ 3
- Python Program to Reverse the Content of a File using Stack
- numpy.stack() in Python
- Python | Stack using Doubly Linked List
- How are variables stored in Python - Stack or Heap?
- How to print exception stack trace in Python?
- Stack in Python
- Reshape a pandas DataFrame using stack,unstack and melt method
- Stack two Pandas series vertically and horizontally
- PyQt5 QCalendarWidget - Moving it to the bottom of the parent stack
- PyQt5 QCalendarWidget - Moving it to the top of the parent stack
- TensorFlow - How to stack a list of rank-R tensors into one rank-(R+1) tensor in parallel
- Important differences between Python 2.x and Python 3.x with examples
- Creating and updating PowerPoint Presentations in Python using python - pptx
- Loops and Control Statements (continue, break and pass) in Python
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Python | Using variable outside and inside the class and method
- Releasing GIL and mixing threads from C and Python
- Python | Boolean List AND and OR operations