{"id":25528081,"date":"2022-06-12T02:09:56","date_gmt":"2022-06-11T20:39:56","guid":{"rendered":"https:\/\/entri.app\/blog\/?p=25528081"},"modified":"2023-04-28T10:51:29","modified_gmt":"2023-04-28T05:21:29","slug":"beginners-guide-to-understand-stacks-in-python","status":"publish","type":"post","link":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/","title":{"rendered":"Beginners Guide To Understand Stacks In Python"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_79_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69e99b90b7687\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69e99b90b7687\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#How_can_we_implement_a_stack_in_Python\" >How can we implement a stack in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#PUSH_Operation\" >PUSH Operation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#POP_Operation\" >POP Operation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#More_about_Usage_of_Stacks_in_Python_and_related_programs\" >More about Usage of Stacks in Python and related programs<\/a><\/li><\/ul><\/nav><\/div>\n<p>A Stack is an Abstract Data Type that stores the order in which items were added to the structure but only allows additions and deletions to the top of the Stack. This follows a Last-In-First-Out (LIFO) principle which does as it says in the name. You can imagine this as the way in which you would eat a Stack of Pancakes (unless you like to eat parts of all the Stack at Once) or the way in which you would use Plates after you washed them after dinner and then were going to reuse them (unless for some reason you wanted to risk breaking them all!).\u00a0These can be used when we want to store outputs in a structure that retains their order but we only want the user to be able to access the last thing they added to that structure. Common examples of this include the back button in your browser which goes to the last item added into the stack or the undo button on your word processor to undo the last addition you made.<\/p>\n<p>When thinking about this we need to be able to define the key methods associated with this data structure. Common operations can include:<\/p>\n<ul>\n<li>push(item): Push an item to the top of the stack<\/li>\n<li>pop(): Remove the top item of the stack and return it<\/li>\n<li>peek(): Return the top item of the stack without removing it<\/li>\n<li>is_empty(): Return True if the Stack is empty<\/li>\n<li>size(): Return how many items are in the Stack<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"How_can_we_implement_a_stack_in_Python\"><\/span><strong>How can we implement a stack in Python?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In Python, we can implement python stacks by:<\/p>\n<ul>\n<li>Using the\u00a0built-in\u00a0List\u00a0data structure.\u00a0Python\u2019s built-in\u00a0List\u00a0data structure comes with methods to simulate both\u00a0stack\u00a0and\u00a0queue\u00a0operations.<\/li>\n<li>Using the\u00a0deque\u00a0library\u00a0which efficiently provides stack and queue operations in one object.<\/li>\n<li>Using the\u00a0queue.LifoQueue\u00a0Class.<\/li>\n<\/ul>\n<p>As mentioned earlier, we can add items to a stack using the\u00a0\u201cPUSH\u201d\u00a0operation and remove items using the\u00a0\u201cPOP\u201d\u00a0operation.<\/p>\n<p style=\"text-align: center;\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/course\/python-programming-course\/\" target=\"_blank\" rel=\"noopener\">&#8220;Ready to take your python skills to the next level? Sign up for a free demo today!&#8221;<\/a><\/strong><\/p>\n<h2><span class=\"ez-toc-section\" id=\"PUSH_Operation\"><\/span><strong>PUSH Operation<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><strong>Pushing to the stack<\/strong><\/h3>\n<p><strong>Steps<\/strong><\/p>\n<ol>\n<li>Check whether stack is FULL. (top == SIZE-1)<\/li>\n<li>If it is FULL, then terminate the function and throw an error.<\/li>\n<li>If it is NOT FULL, then increment top value by one (top++) and set\u00a0stack[top]\u00a0to\u00a0value\u00a0(\u00a0stack[top] = value).<\/li>\n<\/ol>\n<ol>\n<li><strong>void<\/strong> push(<strong>int<\/strong> value) {<\/li>\n<li><strong>if<\/strong>(top == SIZE-1)<\/li>\n<li>printf(&#8220;\\nOverflow. Stack is Full&#8221;);<\/li>\n<li><strong>else<\/strong>{<\/li>\n<li>top++;<\/li>\n<li>stack[top] = value;<\/li>\n<li>printf(&#8220;\\nInsertion was successful&#8221;);<\/li>\n<li>}<\/li>\n<li>}<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>Push\u00a0\u2013 adds an element at the top of the stack. Refer the below image for more understanding:<\/p>\n<h2><span class=\"ez-toc-section\" id=\"POP_Operation\"><\/span><strong>POP Operation<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Pop\u00a0\u2013 removes an element from the top of the stack.<\/p>\n<p><strong>Here is a simple program to illustrating Stack in Python-\u00a0<\/strong><\/p>\n<table width=\"745\">\n<tbody>\n<tr>\n<td width=\"0\">1<\/p>\n<p>2<\/p>\n<p>3<\/p>\n<p>4<\/p>\n<p>5<\/p>\n<p>6<\/p>\n<p>7<\/p>\n<p>8<\/p>\n<p>9<\/p>\n<p>10<\/p>\n<p>11<\/p>\n<p>12<\/p>\n<p>13<\/p>\n<p>14<\/p>\n<p>15<\/p>\n<p>16<\/p>\n<p>17<\/p>\n<p>18<\/p>\n<p>19<\/p>\n<p>20<\/p>\n<p>21<\/p>\n<p>22<\/p>\n<p>23<\/p>\n<p>24<\/p>\n<p>25<\/td>\n<td width=\"700\"><strong>class<\/strong> Stack<\/p>\n<p>def_init_(self):<\/p>\n<p>self.items<strong>=<\/strong>[]\n<p><strong>def<\/strong> is_empty(self):<\/p>\n<p><strong>return<\/strong> self.items<strong>==<\/strong>[]\n<p><strong>def<\/strong> push(self, data):<\/p>\n<p>self.items.append(data)<\/p>\n<p><strong>def<\/strong> pop(self):<\/p>\n<p><strong>return<\/strong> self.items.pop()<\/p>\n<p>s<strong>=<\/strong> Stack()<\/p>\n<p><strong>while<\/strong> True:<\/p>\n<p>print(\u2018push&lt;value&gt;\u2019)<\/p>\n<p>print(\u2018pop\u2019)<\/p>\n<p>print(\u2018quit\u2019)<\/p>\n<p>do<strong>=<\/strong> input(\u2018What would you like to do?\u2019).split()<\/p>\n<p>operation<strong>=<\/strong> do[0].strip().lower()<\/p>\n<p><strong>if<\/strong> operation<strong>==<\/strong> \u2018push\u2019:<\/p>\n<p>s.push(int(do[1]))<\/p>\n<p><strong>elif<\/strong> operation<strong>==<\/strong> \u2018pop\u2019:<\/p>\n<p><strong>if<\/strong> s.is_empty():<\/p>\n<p>print(\u2018Stack <strong>is<\/strong> empty\u2019)<\/p>\n<p><strong>else<\/strong>:<\/p>\n<p>print(\u2018Popped value:\u2019, s.pop())<\/p>\n<p><strong>elif<\/strong> operation<strong>==<\/strong>\u2019quit\u2019:<\/p>\n<p><strong>break<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"More_about_Usage_of_Stacks_in_Python_and_related_programs\"><\/span><strong>More about Usage of Stacks in Python and related programs<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Stacks provide a wide range of uses in algorithms, for eg, in language parsing and\u00a0run-time\u00a0memory management (\u201ccall stack\u201d). A short and useful algorithm using a stack is a\u00a0depth-first search (DFS)\u00a0on a tree or graph data structure.\u00a0Python plays with several stack implementations and each has slightly different characteristics. Let\u2019s take a brief look at them:<\/p>\n<p><strong>\u00a0The\u00a0list\u00a0Built-in<\/strong><\/p>\n<p>Python\u2019s built-in\u00a0list\u00a0type makes a decent stack data structure as it supports push and pop operations in amortized\u00a0O(1)\u00a0time.<\/p>\n<p>Python\u2019s lists are implemented as dynamic arrays internally which means they occasionally need to resize the storage space for elements stored in them whenever they are added or removed. The storage space is allocated more than required,\u00a0so that not every push or pop requires resizing and you get an amortized\u00a0O(1)\u00a0time complexity for these operations.<\/p>\n<p>Although this makes their performance less consistent than the stable\u00a0O(1)\u00a0inserts and deletes provided by a linked list-based implementation. On the other hand, lists provide fast\u00a0O(1)\u00a0time random access to elements on the stack which can be an added benefit.<\/p>\n<p>Here\u2019s an\u00a0important performance caveat\u00a0while using lists as stacks:<\/p>\n<p>To get the amortized\u00a0O(1)\u00a0performance for insertion and deletion, new is added to the end of the list with the\u00a0append()\u00a0method and are removed from the end using\u00a0pop(). Stacks based on\u00a0Python\u00a0lists extend to the right and shrink to the left.<\/p>\n<p>Adding and removing from the front takes much more time \u00a0(O(n)\u00a0time), as the existing elements have to be shifted around to make room for the new element to be added.<\/p>\n<p style=\"text-align: center;\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/course\/python-programming-course\/\" target=\"_blank\" rel=\"noopener\">&#8220;Experience the power of our web development course with a free demo &#8211; enroll now!&#8221;<\/a><\/strong><\/p>\n<p># Using a Python list as a stack (LIFO):<\/p>\n<table width=\"745\">\n<tbody>\n<tr>\n<td width=\"0\">1<\/p>\n<p>2<\/p>\n<p>3<\/p>\n<p>4<\/p>\n<p>5<\/p>\n<p>6<\/p>\n<p>7<\/p>\n<p>8<\/p>\n<p>9<\/p>\n<p>10<\/p>\n<p>11<\/p>\n<p>12<\/p>\n<p>13<\/p>\n<p>14<\/p>\n<p>15<\/p>\n<p>16<\/p>\n<p>17<\/p>\n<p>18<\/td>\n<td width=\"700\">s <strong>=<\/strong> []\n<p>&nbsp;<\/p>\n<p>s.append(&#8216;eat&#8217;)<\/p>\n<p>s.append(&#8216;sleep&#8217;)<\/p>\n<p>s.append(&#8216;code&#8217;)<\/p>\n<p>&nbsp;<\/p>\n<p>&gt;&gt;&gt; s<\/p>\n[&#8216;eat&#8217;, &#8216;sleep&#8217;, &#8216;code&#8217;]\n<p>&nbsp;<\/p>\n<p>&gt;&gt;&gt; s.pop()<\/p>\n<p>&#8216;code&#8217;<\/p>\n<p>&gt;&gt;&gt; s.pop()<\/p>\n<p>&#8216;sleep&#8217;<\/p>\n<p>&gt;&gt;&gt; s.pop()<\/p>\n<p>&#8216;eat&#8217;<\/p>\n<p>&nbsp;<\/p>\n<p>&gt;&gt;&gt; s.pop()<\/p>\n<p>IndexError: &#8220;pop from empty list&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>We know that a list is mutable, it can be changed, and we can easily and simply add and remove items from the beginning and end using the already inbuilt functionality of the list to implement a Stack.<\/p>\n<p>In using a list we have to define a class that uses this but only has specific functionality that allows the user to interact with it in the way that we want. For this, we can use Python\u2019s\u00a0<strong>class<\/strong>\u00a0structure to be able to define the information that the data structure will contain and also the methods that we want to use.<\/p>\n<p>The first thing to do then is to name the\u00a0<strong>class<\/strong>\u00a0as\u00a0Stack\u00a0and create the constructor for this new class. For this, what we want is once a new instance of the object is created is to create an empty list that could be accessed using the\u00a0.items\u00a0attribute. We can instantiate the class as follows:<\/p>\n<p>This means that when an instance of the\u00a0Stack\u00a0class is created then the\u00a0item\u00a0attribute will represent an empty list.<\/p>\n<p>We can then start to add the main functionality to our\u00a0Stack\u00a0<strong>class<\/strong>. The first thing we want to be able to do is to add an item to the end of our stack. The good thing about lists is that there is an inbuilt method for this in the\u00a0.append()\u00a0method. The benefit of this is that it has a constant time complexity O(1) meaning that the time it takes to run will not change with the number of items already in the list because it only adds the item to the end. We can thus take advantage of this to push an item to the end of our\u00a0Stack\u00a0as follows:<\/p>\n<p>Nice and simple right! In this instance we don\u2019t care about what exactly we are adding to the end of the stack, just that we can add it. You can add more functionality to this but for now this is fine.<\/p>\n<p>What about removing items from the Stack? For this, we first of all need to check that the Stack isn\u2019t empty as you can\u2019t remove items from an empty Stack right? If the stack is empty then we can simply return\u00a0none\u00a0so that we do actually return something, but if it is not empty then we can remove the final item and return it. Since we are using Python\u2019s inbuilt list class, we can use the\u00a0.pop()\u00a0method which in Python 3.6+ removes the last item from a list and returns it. This also runs in constant time O(1) because we are only removing the last item and not affecting any other index. We can implement this as:<\/p>\n<p>The benefit of checking whether a list exists first before popping ensures that an error isn\u2019t thrown which could otherwise stop your code from running if it is not handled. Putting this in the actual implementation of the Data Structure just ensures that if you didn\u2019t think of it when running the code using this Data Structure then the code keeps running to some degree.<\/p>\n<p>We now have our main methods associated with a stack but we can also add some additional functionality just to make it more useful for our end-user.<\/p>\n<p>The first thing we can add is a method to peek at the final value in a stack, allowing the user to look at the value before we decide to remove it or not. This can follow a similar implementation tothe\u00a0pop()\u00a0method but instead of using\u00a0.pop()we access the item with the last index in the list. Again, this is has a time complexity of O(1) as we are accessing an item from a list using an index making this nice and simple.<\/p>\n<p>We can also provide a method of checking whether the Stack is empty or not. This would simply return a boolean value of\u00a0True\u00a0if the stack is empty and\u00a0False\u00a0if it is not empty. This also runs in constant time as it is simply checking whether the list within the stack exists or not.<\/p>\n<p>Finally, we can create a method that returns the size of the stack. This tells us the length of the stack, just like it would for a list, and allows the user to see how many items have been added.<\/p>\n<p>We now have the main functionality of our Stack class and most of the additional functionality as well. This just makes it easier and simpler for our end user to interact with this Data structure in the final interface that we created.<\/p>\n<p>The only issue is, if we try to print an instance of the class we get the ugly output of<\/p>\n<p>&lt;__main__.Stack object at 0x0000027F2C960FD0&gt;<\/p>\n<p>which doesn&#8217;t look good and also doesn\u2019t tell us much about the actual instance of the class. What can do then is to use the special\u00a0__str__\u00a0dunder method of the class to tell the interpreter how we want to print out an instance of the class. In this case we simply want to return the entire list contained within the stack which can be implemented as:<\/p>\n<p>The final thing to do then is to put all of this together so that we can create instances of this Stack class in our code.<\/p>\n<p style=\"text-align: center;\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/course\/python-programming-course\/\" target=\"_blank\" rel=\"noopener\">&#8220;Get hands-on with our python course &#8211; sign up for a free demo!&#8221;<\/a><\/strong><\/p>\n<p><strong>The\u00a0collections.deque\u00a0Class<\/strong><\/p>\n<p>The\u00a0deque\u00a0class implements a double-ended queue which supports addition and removal of elements from either end in\u00a0O(1)\u00a0time (non-amortized).<\/p>\n<p>Because deques support adding and removing elements from both ends equally well, they can serve both as queues and as stacks.<\/p>\n<p>Python\u2019s\u00a0deque objects are implemented as doubly-linked lists\u00a0which gives them proper and consistent performance insertion and deletion of elements, but poor\u00a0O(n)\u00a0performance as they randomly access elements in the middle of the stack.<\/p>\n<p>collections.deque\u00a0is a favourable choice if you\u2019re looking for a stack data structure in Python\u2019s standard library with the performance characteristics of a linked-list implementation.<\/p>\n<p># Using collections.deque as a stack (LIFO):<\/p>\n<table width=\"745\">\n<tbody>\n<tr>\n<td width=\"0\">1<\/p>\n<p>2<\/p>\n<p>3<\/p>\n<p>4<\/p>\n<p>5<\/p>\n<p>6<\/p>\n<p>7<\/p>\n<p>8<\/p>\n<p>9<\/p>\n<p>10<\/p>\n<p>11<\/p>\n<p>12<\/p>\n<p>13<\/p>\n<p>14<\/p>\n<p>15<\/p>\n<p>16<\/p>\n<p>17<\/p>\n<p>18<\/p>\n<p>19<\/td>\n<td width=\"700\"><strong>from<\/strong> collections <strong>import<\/strong> deque<\/p>\n<p>q <strong>=<\/strong> deque()<\/p>\n<p>&nbsp;<\/p>\n<p>q.append(&#8216;eat&#8217;)<\/p>\n<p>q.append(&#8216;sleep&#8217;)<\/p>\n<p>q.append(&#8216;code&#8217;)<\/p>\n<p>&nbsp;<\/p>\n<p>&gt;&gt;&gt; q<\/p>\n<p>deque([&#8216;eat&#8217;, &#8216;sleep&#8217;, &#8216;code&#8217;])<\/p>\n<p>&nbsp;<\/p>\n<p>&gt;&gt;&gt; q.pop()<\/p>\n<p>&#8216;code&#8217;<\/p>\n<p>&gt;&gt;&gt; q.pop()<\/p>\n<p>&#8216;sleep&#8217;<\/p>\n<p>&gt;&gt;&gt; q.pop()<\/p>\n<p>&#8216;eat&#8217;<\/p>\n<p>&nbsp;<\/p>\n<p>&gt;&gt;&gt; q.pop()<\/p>\n<p>IndexError: &#8220;pop from an empty deque&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>The\u00a0queue.LifoQueue\u00a0Class<\/strong><\/p>\n<p>This stack implementation in the Python standard library is synchronized and provides locking semantics to support multiple concurrent producers and consumers.<\/p>\n<p>The\u00a0queue\u00a0module\u00a0contains several other classes implementing multi-producer, multi-consumer queues that are useful for parallel computing.<\/p>\n<p>Depending on your use case the locking semantics might be helpful, or just incur unneeded overhead. In this case you\u2019d be better off with using a\u00a0list\u00a0or a\u00a0deque\u00a0as a general purpose stack.<\/p>\n<p># Using\u00a0 queue.LifoQueue as a stack:<\/p>\n<table width=\"745\">\n<tbody>\n<tr>\n<td width=\"0\">1<\/p>\n<p>2<\/p>\n<p>3<\/p>\n<p>4<\/p>\n<p>5<\/p>\n<p>6<\/p>\n<p>7<\/p>\n<p>8<\/p>\n<p>9<\/p>\n<p>10<\/p>\n<p>11<\/p>\n<p>12<\/p>\n<p>13<\/p>\n<p>14<\/p>\n<p>15<\/p>\n<p>16<\/p>\n<p>17<\/p>\n<p>18<\/p>\n<p>19<\/p>\n<p>20<\/p>\n<p>21<\/p>\n<p>22<\/td>\n<td width=\"700\"><strong>from<\/strong> queue <strong>import<\/strong> LifoQueue<\/p>\n<p>s <strong>=<\/strong> LifoQueue()<\/p>\n<p>&nbsp;<\/p>\n<p>s.put(&#8216;eat&#8217;)<\/p>\n<p>s.put(&#8216;sleep&#8217;)<\/p>\n<p>s.put(&#8216;code&#8217;)<\/p>\n<p>&nbsp;<\/p>\n<p>&gt;&gt;&gt; s<\/p>\n<p>&lt;queue.LifoQueue object at 0x108298dd8&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>&gt;&gt;&gt; s.get()<\/p>\n<p>&#8216;code&#8217;<\/p>\n<p>&gt;&gt;&gt; s.get()<\/p>\n<p>&#8216;sleep&#8217;<\/p>\n<p>&gt;&gt;&gt; s.get()<\/p>\n<p>&#8216;eat&#8217;<\/p>\n<p>&nbsp;<\/p>\n<p>&gt;&gt;&gt; s.get_nowait()<\/p>\n<p>queue.Empty<\/p>\n<p>&nbsp;<\/p>\n<p>&gt;&gt;&gt; s.get()<\/p>\n<p># Blocks \/ waits forever&#8230;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"text-align: center;\">\n<h4><strong>Related Articles<\/strong><\/h4>\n<div class=\"table-responsive wprt_style_display\">\n<div class=\"table-responsive wprt_style_display\">\n<div class=\"table-responsive wprt_style_display\">\n<div class=\"table-responsive wprt_style_display\">\n<div class=\"table-responsive wprt_style_display\">\n<table class=\"table\" dir=\"ltr\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<colgroup>\n<col width=\"329\" \/>\n<col width=\"309\" \/><\/colgroup>\n<tbody>\n<tr>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Syllabus&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-village-field-assistant-vfa-syllabus-exam-pattern\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/data-visualization-in-python\/\" target=\"_blank\" rel=\"noopener\">Introduction to Data Visualization in Python<\/a><\/strong><\/td>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Mock Test&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-vfa-free-mock-test\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/why-python-is-used-for-data-science\/\" target=\"_blank\" rel=\"noopener\">Why Python Is Used For Data Science?<\/a><\/strong><\/td>\n<\/tr>\n<tr>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Exam Date&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-vfa-exam-date\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/step-by-step-guide-for-getting-a-job-as-a-python-developer\/\" target=\"_blank\" rel=\"noopener\">Guide for getting a job as a Python Developer<\/a><\/strong><\/td>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Video Course&quot;}\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/top-python-interview-questions-and-answers\/\" target=\"_blank\" rel=\"noopener\">Python Advanced Interview Questions and Answers<\/a><\/strong><\/td>\n<\/tr>\n<tr>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Application Form&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-vfa-apply-online\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/python-online-course\/\" target=\"_blank\" rel=\"noopener\">Best Online Python Course with Certificate<\/a><\/strong><\/td>\n<td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Kerala PSC VFA Study Materials&quot;}\" data-sheets-hyperlink=\"https:\/\/entri.app\/blog\/kerala-psc-vfa-study-material\/\"><strong><a class=\"in-cell-link\" href=\"https:\/\/entri.app\/blog\/type-conversion-in-python\/\" target=\"_blank\" rel=\"noopener\">What is Type Conversion in Python?<\/a><\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A Stack is an Abstract Data Type that stores the order in which items were added to the structure but only allows additions and deletions to the top of the Stack. This follows a Last-In-First-Out (LIFO) principle which does as it says in the name. You can imagine this as the way in which you [&hellip;]<\/p>\n","protected":false},"author":55,"featured_media":25528084,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[802,558,1888],"tags":[],"class_list":["post-25528081","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","category-general-knowledge","category-python-programming"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Beginners Guide To Understand Stacks In Python - Entri Blog<\/title>\n<meta name=\"description\" content=\"Stacks In Python - A Stack is an Abstract Data Type that stores the order in which items were added to the structure but only allows additions and deletions to the top of the Stack.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Beginners Guide To Understand Stacks In Python - Entri Blog\" \/>\n<meta property=\"og:description\" content=\"Stacks In Python - A Stack is an Abstract Data Type that stores the order in which items were added to the structure but only allows additions and deletions to the top of the Stack.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Entri Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/entri.me\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-11T20:39:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-28T05:21:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/Beginners-Guide-To-Understand-Stacks-In-Python.png\" \/>\n\t<meta property=\"og:image:width\" content=\"820\" \/>\n\t<meta property=\"og:image:height\" content=\"615\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ayesha Surayya\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@entri_app\" \/>\n<meta name=\"twitter:site\" content=\"@entri_app\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ayesha Surayya\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/\"},\"author\":{\"name\":\"Ayesha Surayya\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/person\/568cc9d6e77fd5d01033b61c88343097\"},\"headline\":\"Beginners Guide To Understand Stacks In Python\",\"datePublished\":\"2022-06-11T20:39:56+00:00\",\"dateModified\":\"2023-04-28T05:21:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/\"},\"wordCount\":2178,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/entri.app\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/Beginners-Guide-To-Understand-Stacks-In-Python.png\",\"articleSection\":[\"Articles\",\"General Knowledge\",\"Python Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/\",\"url\":\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/\",\"name\":\"Beginners Guide To Understand Stacks In Python - Entri Blog\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/Beginners-Guide-To-Understand-Stacks-In-Python.png\",\"datePublished\":\"2022-06-11T20:39:56+00:00\",\"dateModified\":\"2023-04-28T05:21:29+00:00\",\"description\":\"Stacks In Python - A Stack is an Abstract Data Type that stores the order in which items were added to the structure but only allows additions and deletions to the top of the Stack.\",\"breadcrumb\":{\"@id\":\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#primaryimage\",\"url\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/Beginners-Guide-To-Understand-Stacks-In-Python.png\",\"contentUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/Beginners-Guide-To-Understand-Stacks-In-Python.png\",\"width\":820,\"height\":615,\"caption\":\"Beginners Guide To Understand Stacks In Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/entri.app\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Programming\",\"item\":\"https:\/\/entri.app\/blog\/category\/python-programming\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Beginners Guide To Understand Stacks In Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/entri.app\/blog\/#website\",\"url\":\"https:\/\/entri.app\/blog\/\",\"name\":\"Entri Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/entri.app\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/entri.app\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/entri.app\/blog\/#organization\",\"name\":\"Entri App\",\"url\":\"https:\/\/entri.app\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png\",\"contentUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png\",\"width\":989,\"height\":446,\"caption\":\"Entri App\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/entri.me\/\",\"https:\/\/x.com\/entri_app\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/person\/568cc9d6e77fd5d01033b61c88343097\",\"name\":\"Ayesha Surayya\",\"url\":\"https:\/\/entri.app\/blog\/author\/ayesha-surayya\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Beginners Guide To Understand Stacks In Python - Entri Blog","description":"Stacks In Python - A Stack is an Abstract Data Type that stores the order in which items were added to the structure but only allows additions and deletions to the top of the Stack.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Beginners Guide To Understand Stacks In Python - Entri Blog","og_description":"Stacks In Python - A Stack is an Abstract Data Type that stores the order in which items were added to the structure but only allows additions and deletions to the top of the Stack.","og_url":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/","og_site_name":"Entri Blog","article_publisher":"https:\/\/www.facebook.com\/entri.me\/","article_published_time":"2022-06-11T20:39:56+00:00","article_modified_time":"2023-04-28T05:21:29+00:00","og_image":[{"width":820,"height":615,"url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/Beginners-Guide-To-Understand-Stacks-In-Python.png","type":"image\/png"}],"author":"Ayesha Surayya","twitter_card":"summary_large_image","twitter_creator":"@entri_app","twitter_site":"@entri_app","twitter_misc":{"Written by":"Ayesha Surayya","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#article","isPartOf":{"@id":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/"},"author":{"name":"Ayesha Surayya","@id":"https:\/\/entri.app\/blog\/#\/schema\/person\/568cc9d6e77fd5d01033b61c88343097"},"headline":"Beginners Guide To Understand Stacks In Python","datePublished":"2022-06-11T20:39:56+00:00","dateModified":"2023-04-28T05:21:29+00:00","mainEntityOfPage":{"@id":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/"},"wordCount":2178,"commentCount":0,"publisher":{"@id":"https:\/\/entri.app\/blog\/#organization"},"image":{"@id":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/Beginners-Guide-To-Understand-Stacks-In-Python.png","articleSection":["Articles","General Knowledge","Python Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/","url":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/","name":"Beginners Guide To Understand Stacks In Python - Entri Blog","isPartOf":{"@id":"https:\/\/entri.app\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#primaryimage"},"image":{"@id":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/Beginners-Guide-To-Understand-Stacks-In-Python.png","datePublished":"2022-06-11T20:39:56+00:00","dateModified":"2023-04-28T05:21:29+00:00","description":"Stacks In Python - A Stack is an Abstract Data Type that stores the order in which items were added to the structure but only allows additions and deletions to the top of the Stack.","breadcrumb":{"@id":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#primaryimage","url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/Beginners-Guide-To-Understand-Stacks-In-Python.png","contentUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/06\/Beginners-Guide-To-Understand-Stacks-In-Python.png","width":820,"height":615,"caption":"Beginners Guide To Understand Stacks In Python"},{"@type":"BreadcrumbList","@id":"https:\/\/entri.app\/blog\/beginners-guide-to-understand-stacks-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/entri.app\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Programming","item":"https:\/\/entri.app\/blog\/category\/python-programming\/"},{"@type":"ListItem","position":3,"name":"Beginners Guide To Understand Stacks In Python"}]},{"@type":"WebSite","@id":"https:\/\/entri.app\/blog\/#website","url":"https:\/\/entri.app\/blog\/","name":"Entri Blog","description":"","publisher":{"@id":"https:\/\/entri.app\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/entri.app\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/entri.app\/blog\/#organization","name":"Entri App","url":"https:\/\/entri.app\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png","contentUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png","width":989,"height":446,"caption":"Entri App"},"image":{"@id":"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/entri.me\/","https:\/\/x.com\/entri_app"]},{"@type":"Person","@id":"https:\/\/entri.app\/blog\/#\/schema\/person\/568cc9d6e77fd5d01033b61c88343097","name":"Ayesha Surayya","url":"https:\/\/entri.app\/blog\/author\/ayesha-surayya\/"}]}},"_links":{"self":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25528081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/users\/55"}],"replies":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/comments?post=25528081"}],"version-history":[{"count":6,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25528081\/revisions"}],"predecessor-version":[{"id":25559358,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25528081\/revisions\/25559358"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media\/25528084"}],"wp:attachment":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media?parent=25528081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/categories?post=25528081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/tags?post=25528081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}