How to implement Stack in python

0 votes

I have 2 issues with the code below:

  1. push(o) throws an exception TypeError: can only assign an iterable.
  2. Should I throw an exception if pop() is invoked on an empty stack ?

    class Stack(object):
    
        def __init__(self):
            self.storage = []
    
        def isEmpty(self):
            return len(self.storage) == 0
    
        def push(self,p):
            self.storage[:0] = p
    
        def pop(self):
            """issue: throw exception?"""
            return None

Nov 15, 2018 in Python by Jino
• 5,820 points
1,375 views

2 answers to this question.

0 votes
class Stack:
  def __init__(self):
    self.__storage = []

  def isEmpty(self):
    return len(self.__storage) == 0

  def push(self,p):
    self.__storage.append(p)

  def pop(self):
    return self.__storage.pop()

This way you can securely implement all the functions of stack.

answered Nov 15, 2018 by findingbugs
• 3,260 points
0 votes

There are different method to implement stack in python

one of theme i am explain in detailed

# Python program to 
# demonstrate stack implementation 
# using list 


stack = [] 

# append() function to push 
# element in the stack 
stack.append('a') 
stack.append('b') 
stack.append('c') 

print('Initial stack') 
print(stack) 

# pop() fucntion to pop 
# element from stack in 
# LIFO order 
print('\nElements poped from stack:') 
print(stack.pop()) 
print(stack.pop()) 
print(stack.pop()) 

print('\nStack after elements are poped:') 
print(stack) 

# uncommenting print(stack.pop()) 
# will cause an IndexError
# as the stack is now empty

OUTPUT

Initial stack
['a', 'b', 'c']

Elements poped from stack:
c
b
a

Stack after elements are poped:
[]

Best of luck!!!!

answered Jun 21, 2020 by pagarsach
• 160 points

Related Questions In Python

0 votes
1 answer

How to implement Hashmaps in Python

Python dictionary is a built-in type that supports ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,720 points
1,108 views
0 votes
1 answer

“stub” __objclass__ in a Python class how to implement it?

You want to avoid interfering with this ...READ MORE

answered Sep 27, 2018 in Python by Priyaj
• 58,020 points
2,369 views
0 votes
1 answer

How to implement Linked List in Python?

You can use Deque that works better than linked list ...READ MORE

answered Oct 26, 2018 in Python by Priyaj
• 58,020 points
1,070 views
0 votes
1 answer

How to implement Queue in python

You are missing this  from queue import * This ...READ MORE

answered Nov 2, 2018 in Python by Nabarupa
3,116 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,463 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,120 views
0 votes
1 answer

How to write to CSV line by line in python

The simple way of doing this will ...READ MORE

answered Nov 15, 2018 in Python by findingbugs
• 3,260 points
5,678 views
0 votes
1 answer

How to do Matrix Multiplication in python?

def matmult(a,b): zip_b = ...READ MORE

answered Nov 15, 2018 in Python by findingbugs
• 3,260 points
1,989 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP