- [Instructor] We've now looked at most of the methods of
the list type in Python, but there is another method
that we haven't taken a look at yet, which is
the pop method.
So if you're familiar with data structures like a stacked
data structure or a queue data structure,
a stack is a Last In First Out data structure.
A queue is a First In First Out data structure.
You can actually implement those types of data structures
using the methods that I've shown you so far
and the pop method that I'm about to demonstrate
for you as well.
So, if, for example, I were to create a stack variable,
which is just an empty list, I can go ahead and start
appending items to that list.
So let's say I append "red".
Now at this point, "red" is the top of the stack,
if we're maintaining the list as a Last in First Out
data structure.
So we can go ahead and see what the stack contains
at the moment, and let's add another item.
So at this point, we'll add "green", and if we evaluate
the stack again, "green" is at the end of the list.
So, so far we're adding to the end only.
If we then remove from the end, that would be
the top element of the stack, and the last item I added,
"green", would be the first item I remove.
So we can use the pop method that way.
So popping the stack gives me green.
Popping the stack gives me "red".
At this point, the stack is empty, but if I were to
try to pop the stack again, you can see that I get
an exception in that case.
An index error indicating that I tried to pop from
an empty list, which is not allowed, and therefore
you get the exception in that particular case.
So, as you saw here, you can certainly mimic the concept
of a stacked data structure and it would be easy to do
something similar to mimic a queue data structure as well.