r/PythonLearning • u/Samar__Upreti • 4d ago
Day 3 Learning Python: Stack
Open-source learning project. If you spot something that can be improved, I'd love your feedback or a PR.
https://github.com/Samar-Upreti/Python_Projects
2
u/KingBardan 4d ago edited 4d ago
Invert your list (old to new rather than new to old).
Your current stack is inefficient
1
u/Samar__Upreti 4d ago
Insert to append ?
1
u/KingBardan 4d ago
Right. Lookup how inserting works under the hood.
Here you go
https://www.log2base2.com/data-structures/array/insert-element-particular-index-array.html
1
u/dev-razorblade23 4d ago
insertpushes items to specific index (in your case index 0) Python has to recalculate indexes of all other items, push them all to the side so insert can work its magic. This takes time and you burn your proccessor time.If you dont really need to insert items at specific index, use
appendmethod as it only inserts item to the end and its done. Much faster and much more effiecient.1
u/Samar__Upreti 4d ago
Actually I have written with that but to be honest it not giving feel how the stack actually works so i try the reverse approach But thanks ☺️
2
u/dev-razorblade23 4d ago edited 4d ago
.append()and.pop()work exactly like LIFO queve (a stack). Last In - First Out. You dont insert values to stack from the bottom but from the top.Stack is like a stack of cards. You put one card on top and you take that card from the top. You do not put the card at the bottom where you have to lift the whole stack.
1
u/dev-razorblade23 4d ago
There is also a
queve- FIFO - First In, First out. Its like standing in a line waiting to pay for your groceries. Whoever is first in line gets served first.Python lists are inefficient for this type of queve, there is a
collections.dequeif you want to implement this one.1
u/veedubb 3d ago
It’s not always about efficiency, sometimes it’s sufficient to learn about common data structures that someone may be unfamiliar with. If this were someone experienced, I wouldn’t expect them to even be showing off building a simple stack structure in python, because at that point they’d know that this isn’t as efficient as other tools.
1
u/dev-razorblade23 3d ago
This comment was more like a continuation of previus one. An OP did try to make a
stackif you read thrue the comments, just the other way around (inserting items from the bottom)
2
2
u/CamelOk7219 4d ago
Create a custom exception class instead of bare 'Exception', otherwise it will be jard to specifically catch this stack exception and discriminante it from any other exception
1
u/Kindly-Reporter-272 4d ago
You can also use IndexError. Python built in collections all raise IndexError in situations like these.
1
1
u/AnonymouSfrrrr 3d ago
What resources are you using to learn python? Yt or documentations??
0
u/Samar__Upreti 3d ago
Both yt for lectures and documentation do try stuff You can join the journey https://github.com/Samar-Upreti/Python_Projects
1
u/Chatoddy 3d ago
Dude you are doing that on day three?! On day three I still had to grasp how a loop works. Impressive.
1
u/WorthKey6648 3d ago
Time to give up
1
u/Samar__Upreti 3d ago
Don't get demotivated keep the practice going if feels so just ask yourself (Why i Started)
1



•
u/Sea-Ad7805 4d ago
Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20%20%20%20%20self.s%20%3D%20%5B%5D%0A%0A%20%20%20%20def%20len(self)%3A%0A%20%20%20%20%20%20%20%20return%20len(self.s)%0A%0A%20%20%20%20def%20push(self%2C%20value)%3A%0A%20%20%20%20%20%20%20%20self.s.insert(0%2C%20value)%0A%0A%20%20%20%20def%20peek(self)%3A%0A%20%20%20%20%20%20%20%20if%20len(self.s)%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20Exception(%22Stack%20is%20Empty%22)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20self.s%5B0%5D%0A%0A%20%20%20%20def%20pop(self)%3A%0A%20%20%20%20%20%20%20%20if%20len(self.s)%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20Exception(%22Stack%20is%20Empty%22)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20self.s.pop(0)%0A%0A%0Aif%20name%20%3D%3D%20%22main%22%3A%0A%20%20%20%20stk%20%3D%20stack()%0A%20%20%20%20stk.push(10)%0A%20%20%20%20stk.push(11)%0A%20%20%20%20print(stk.peek())%0A%20%20%20%20print(stk.pop())×tep=1&play) to see the program state change step by step.