Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the last element of a python list?

Tags:

python

stack

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.

like image 223
sekmet64 Avatar asked Nov 03 '10 08:11

sekmet64


1 Answers

No need to slice.

stack[-1]
like image 86
Ignacio Vazquez-Abrams Avatar answered Oct 07 '22 01:10

Ignacio Vazquez-Abrams