1 00:00:00,660 --> 00:00:03,180 - [Instructor] We've now looked at most of the methods of 2 00:00:03,180 --> 00:00:06,230 the list type in Python, but there is another method 3 00:00:06,230 --> 00:00:08,230 that we haven't taken a look at yet, which is 4 00:00:08,230 --> 00:00:10,020 the pop method. 5 00:00:10,020 --> 00:00:14,000 So if you're familiar with data structures like a stacked 6 00:00:14,000 --> 00:00:16,541 data structure or a queue data structure, 7 00:00:16,541 --> 00:00:20,350 a stack is a Last In First Out data structure. 8 00:00:20,350 --> 00:00:23,710 A queue is a First In First Out data structure. 9 00:00:23,710 --> 00:00:27,200 You can actually implement those types of data structures 10 00:00:27,200 --> 00:00:30,220 using the methods that I've shown you so far 11 00:00:30,220 --> 00:00:33,670 and the pop method that I'm about to demonstrate 12 00:00:33,670 --> 00:00:35,070 for you as well. 13 00:00:35,070 --> 00:00:40,070 So, if, for example, I were to create a stack variable, 14 00:00:40,620 --> 00:00:43,890 which is just an empty list, I can go ahead and start 15 00:00:43,890 --> 00:00:46,410 appending items to that list. 16 00:00:46,410 --> 00:00:48,510 So let's say I append "red". 17 00:00:48,510 --> 00:00:51,790 Now at this point, "red" is the top of the stack, 18 00:00:51,790 --> 00:00:56,210 if we're maintaining the list as a Last in First Out 19 00:00:56,210 --> 00:00:57,220 data structure. 20 00:00:57,220 --> 00:00:59,840 So we can go ahead and see what the stack contains 21 00:00:59,840 --> 00:01:02,760 at the moment, and let's add another item. 22 00:01:02,760 --> 00:01:07,090 So at this point, we'll add "green", and if we evaluate 23 00:01:07,090 --> 00:01:09,960 the stack again, "green" is at the end of the list. 24 00:01:09,960 --> 00:01:12,610 So, so far we're adding to the end only. 25 00:01:12,610 --> 00:01:15,180 If we then remove from the end, that would be 26 00:01:15,180 --> 00:01:18,827 the top element of the stack, and the last item I added, 27 00:01:18,827 --> 00:01:21,750 "green", would be the first item I remove. 28 00:01:21,750 --> 00:01:24,363 So we can use the pop method that way. 29 00:01:25,390 --> 00:01:28,080 So popping the stack gives me green. 30 00:01:28,080 --> 00:01:30,580 Popping the stack gives me "red". 31 00:01:30,580 --> 00:01:34,050 At this point, the stack is empty, but if I were to 32 00:01:34,050 --> 00:01:38,230 try to pop the stack again, you can see that I get 33 00:01:38,230 --> 00:01:39,640 an exception in that case. 34 00:01:39,640 --> 00:01:43,110 An index error indicating that I tried to pop from 35 00:01:43,110 --> 00:01:45,840 an empty list, which is not allowed, and therefore 36 00:01:45,840 --> 00:01:49,560 you get the exception in that particular case. 37 00:01:49,560 --> 00:01:53,880 So, as you saw here, you can certainly mimic the concept 38 00:01:53,880 --> 00:01:56,850 of a stacked data structure and it would be easy to do 39 00:01:56,850 --> 00:02:01,173 something similar to mimic a queue data structure as well.