Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now!

All Courses
Stack In Python

Stack In Python

January 20th, 2020

What is a Stack?

One of the earliest data structures defined in computer science is stacks. It is a collection of objects and it supports Last In First Out (LIFO). There are two operators that can be performed using stack,

  • push – It is used to add data into the stack
  • pop – It is used to remove data from the stack

It is easy to learn stacks and implement them, for carrying out various tasks, stacks can be incorporated in many software for carrying out various tasks. To implement stack we will be relying on the list data structure in python.
There are because the creation of layers of push and pop menu functions is done in the background run the unknown to the everyone user layer memory if can cause “stack overflow”. If the program is not handled properly, assign overlay can create an error message or cause the program to crash.

Why do use a stack?

It is expected to take O(1) time for inserting and deleting operations using stack. In order to stack in a better way, let us consider a pile of books, you add a book on top another book and you continue to do so. In this, the first book to be picked up will be the last book that was added. Understanding the real-world use cases of the stack helps us in solving many data storage problems in an effective and easy way. Suppose, you are working on a new word processor

Push Operation

Push in Stack
Push – It adds elements to the top of the stack.
In the above image, the first stack is having A, B and C. Suppose, we need to add D to it, it gets added on top of the stack

Pop Operation

Pop in Stack
Pop – It removes the element from the top of the stack
In the above image, the first stack is having A, B, C and D. Suppose, we need to remove D from it, it was added last on top of the stack while removing D gets removed first (Last In First Out)

List in Python

We have a variable s with some values. We need to add value to it. This can be done in python using the append() method available in the list.

Python List

s is having 1,2,3 assigned to it. Now, we need to add 4 to it. The above image shows how this task can be accomplished.
We have a variable s with some values. We need to remove the last value in it. This can be done in python using a pop() method available in the list.
Python Pop Link
Using the pop() method, it removes the last element in the list and displays the popped element in the output. Now, if we display the variable s, it will not have the popped value.
pop() can also be used to remove an element in particular index
Collection of Deque
Using a pop() method with some index value, it removes the element in that index and displays the popped element in the output. Now, if we display the variable s, it will not have the popped value.

Collections.deque Class

Collection Deque
In this above code, we are importing the deque from the collections package. We have created a variable q and assigned it is as deque() Now, to add values to q, we can use append(), the same method that we have used in the list to add values to a variable. I have added three values to q using append() and when we print q all the values that are appended to q gets displayed
Deque Function
To remove some values from the variable q, we can use pop() method available. pop() removes the last value from the variable
Pop Value in Stack
In pop() method in the list, we can pass an index and remove that index. But, if a variable is assigned as deque, then pop() does not take in any parameters, if you try to pass an index to remove that particular index, it will throw an error.

Related Blogs