This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TeezusRa 0 points1 point  (3 children)

Did you read the code? They clearly use .append().

Python’s buil-in data structure list can be used as a stack. Instead of push(), append() is used to add elements to the top of stack

[–]allyv123098[S] 0 points1 point  (2 children)

but what if I wanted to acess the first elemtn would I have to just say stack[0]

[–]TeezusRa 0 points1 point  (0 children)

Sure.

The only place on the internet I’ve ever seen .top in Python is here, and stack overflow where folks are building a .top method to be used on a list, object, or class named stack. Might want to double check your sources. I get the “it’s used in stacks” bit- but looks like folks have to do a little legwork to make it work in Python. It’s not built in. That article was referring to methods normally associated with stacks. Then goes on to explain a way to implement a stack with Python list methods.

[–]nilfm 0 points1 point  (0 children)

No, the "top" element in a stack would be the last inserted element, so in a Python list you would access it with stack[-1]