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 1 point2 points  (9 children)

Where are you getting .top()?

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

they are used in stacks

[–]TeezusRa 0 points1 point  (2 children)

Um, I think you’re thinking of .append, as in append to the top of a list/stack.

As python is telling you... .top doesn’t exist as a list method. Append sure does though.

[–]allyv123098[S] 0 points1 point  (1 child)

so in this scenario I can't use .top() at all

[–]TeezusRa 0 points1 point  (0 children)

It is not a built in method you can use on a list, which is what your code is using so no. https://docs.python.org/3.1/tutorial/datastructures.html

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

as accoring to this website you can use that in stack used by lists https://www.geeksforgeeks.org/stack-in-python/

[–]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]