开发者

How to check the last element of a python list?

In python I need a stack, and I'm using a list for it. In the documenation it says that you can use append(开发者_如何学运维) and pop() for stack operations but what about accessing the top of the stack without removing it?

How to do that in the most readable way? Because all I came up with is stack[-1:][0] which looks a bit ugly for me, there must be a better way.


No need to slice.

stack[-1]


stack[-1] ist the last element

EDIT renamed the previously list called variable (Thanks, Tim McNamara).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜