Skip to playerSkip to main content
🐍 Python deque Explained: Efficient Queues and Stacks

In this practical Python programming tutorial, learn how to use deque from Python's built-in collections module to efficiently add and remove elements from both ends of a sequence.

💻 What you'll learn:

What deque is and how it works

The difference between deque and a Python list

Adding and removing elements from both ends

Using deque to implement queues and stacks

When to use deque in real-world Python projects

🚀 Whether you're a beginner or an experienced programmer, understanding deque will help you choose the right data structure for your applications.

💬 Question: Would you use deque or a list to implement a queue? Share your answer in the comments!

Follow for more practical Python programming, data structures, ICT, and cybersecurity tutorials.

#Python #PythonProgramming #Deque #DataStructures #LearnPython #Coding #ProgrammingTutorial

Category

📚
Learning
Transcript
00:01Hello guys, welcome once again.
00:04Today we are going to talk about another important container which is a type called DEC.
00:11DEC is found in the collections module.
00:18So to do that, we will do this in the discord editor.
00:26And I'll have to create a file that I'll be doing the demonstration.
00:31So just use echo command, get this file called DEC demo.py, press enter.
00:47That is my file, when I type DIR I should be able to see it here.
00:51It's called DEC demo.
00:55Okay, so I'll just clear the screen again.
01:01Now, in this file called DEC demo, it is found inside collections module.
01:10So this is my file.
01:12Leave this.
01:17So, have you ever wanted to add or remove an item from both ends, that is the left side and
01:25on the right side efficiently?
01:28So that's what we are going to look at.
01:30How is it possible to remove an item from right or left without facing more challenges?
01:40And that, we are going to look at that using DEC, container, data type.
01:47So, what is this DEC?
01:52What is this DEC?
01:53What is this DEC?
01:56What is this DEC?
02:17DEC?
02:18Let's say I want to add an item at the beginning of the list and also at the end of
02:30the list.
02:31How will that be possible?
02:34So, for a list, you'll have to use insert and specify the index position.
02:40Let's say my list.
02:44Let's say my list.insert.index.position.
02:47Let's say 0.
02:48I want to insert 40.
02:53And then I'll say I want to print my list.
03:00So, I want to insert an item at index position 0.
03:04That is where 10 is.
03:06I want to insert an item 40 in that specific index position.
03:13Let's see where the rest of the items will be pushed to.
03:19So, this is Python.
03:21And we are running DEC demo.py.
03:28You can see 40 has been added on the rightmost side.
03:36But when you look at this, you realize that we had to supply the index position of the item
03:44on the leftmost side.
03:47That meant the item to be pushed on the right.
03:52Now, how can DEC achieve this?
03:58And also, before we even talk about it, how can we add an item at the rightmost side of the
04:05list
04:06without using DEC.
04:09So, this one, this is inserting.
04:16Starting on the left side.
04:20Let's look at how you can insert on the rightmost side.
04:25How can you insert an item on the rightmost side?
04:28So, I'll go and say inserting right side.
04:36So, that will say my list.
04:38And I will use the append method.
04:44Append method by default is written on the rightmost side of the list automatically.
04:53So, I'll say I want to add 70 there.
04:58And so, when I want to display what has happened, I will write my list.
05:07Yeah.
05:09So, I can come back to my code, my CMD and the file.
05:15You can see what was added on the leftmost side, what was added on the rightmost side.
05:24Now, how can we achieve the same, same thing with DEC?
05:32This is how we can achieve it.
05:35So, this is all about the list.
05:37You can see, I have a list of items here.
05:40I used insert method to add item 40 at index position 0.
05:46This is pushed the leftmost item to the right to accommodate the new item.
05:54The next part here, I inserted right.
05:58That meant append method added an item at the rightmost side by default.
06:06Now, with DEC, things are very, very easy.
06:13How?
06:14For example, in order to use DEC in your file, you'll have to import it.
06:22from collections module.
06:28You'll have to import DEC here.
06:32So, this is how you include DEC in your file.
06:37You'll have to import it from collections module.
06:42Now, I will say DEC LIST, DEC LIST.
06:52DEC LIST equals to DEC and then DEC is like a, let's say a function called, where there is brackets
07:14here.
07:14So, it's a function with a return value.
07:18So, what is expected here?
07:20It is the list.
07:21It's applying the list as the argument.
07:24So, let's say, I put some values here.
07:28Then three values.
07:30Now, let me see what is displayed.
07:39Save the file.
07:40Come back to your terminal.
07:43Find the program.
07:45You can see DEC is able to show you the list.
07:48So, basically the list is inserted inside.
07:55Now, how can you add, add item on left side, leftmost side?
08:09How can you add an item on left side using DEC?
08:16I'll come and say DEC.
08:23DEC.
08:26DEC.
08:28Append left.
08:29See?
08:31You don't need to supply the index.
08:34Leftmost item and push it so that you can insert an item there.
08:42Then you push what was originally there, the right side.
08:45So, just add this method, append left.
08:48Inside there, you add the item you want to add to the left.
08:54Then you print your...
09:00When you print your DEC list, it's supposed to be L.
09:11So, when you print it and run, you'll be able to get the item added on the left.
09:21So, how about if you want to add an item on the right.
09:26That way, you just use similar method, item on rightmost side.
09:42I will use the Q L dot end.
09:52So, this time I will say 200 int the Q L.
10:04Then for this, I'll run the program.
10:07As you can see, 200 was added on the rightmost side of the list.
10:19So, that is how a DEC can be used to add an item on the right and then left DEC.
10:32So, removing items.
10:36So, removing item from DEC.
10:41How does that work?
10:43How do you remove an item from?
10:45So, let's say I have this Q.
10:49That is my list.
10:50I will use...
10:54So, I'll say the Q L dot pop.
11:01So, pop by default remove an item on the rightmost side.
11:06That's what I can use here.
11:09Remove the item on the rightmost side.
11:13If I run this, I should be able to see...
11:19My list is...
11:22One item has been created for me.
11:24This.
11:26Okay, I have a problem here.
11:32Let's remove these ones.
11:37Here you have to supply the specific value you want to remove for the item, for the index
11:44position.
11:47But right now I don't have any item I want to remove.
11:51So...
11:51Comment.
11:53Okay.
11:56Turn back.
11:58So, we run the file.
12:05That is not happening.
12:16Let me just remove them here.
12:22And then let me run the file again.
12:26Okay.
12:29So, from here we can see that 200 was removed from...
12:36Take.
12:38We are remaining with this one.
12:40So, we use pop to remove an item on the right...
12:46side by default.
12:49Now, you should want to remove an item on the left side.
12:58So, you want to remove on the left.
13:00So, this is item from the same deck but the right side.
13:08Now, I am about removing item on the left.
13:19Left.
13:21So, say the Q L dot pop left.
13:30With the value here I want to remove.
13:33Left side.
13:35Now, not aside the value.
13:37Yes, the thing about the deck is you don't supply any value, any index position.
13:44Then you want to remove on the left or right most side.
13:48So, just use pop left.
13:51This one will remove the item on the left most side.
13:55So, if I print back, I should be able to get item removed.
14:04So, you can see a hundred is removed.
14:06Here I use pop.
14:08It removes item on the right most side.
14:17So, what are some of the practical examples you can use?
14:26This is how you can use decks.
14:29Let me just create a simple file here.
14:32Let me just say echo example.py.
14:41So, in this file, let's say you have tasks queued for execution that is one by one.
14:58For example, let's say from collections, import, check.
15:11Then I'll say these tasks equals to something like, have something to print.
15:22Let's say it's a report.
15:26Let's say you're supposed to send some email somewhere.
15:35Also, you're supposed to pack up files.
15:39Let's just say, let's add those.
15:45So, what happens, this is your initial tasks.
15:51Your initial tasks.
15:54Let's say before anything happens.
16:00These are your initial tasks.
16:08So, maybe I can run this.
16:14Python.
16:19You can see these are my tasks I have here.
16:23Let's say now I want to.
16:30I want to add a new item.
16:33I want to add new task.
16:38This task can be something like tasks.append.
16:45New task I want to add, it's like need to update some software.
17:01Print and it's the tasks.
17:05If I run this, I should be able to get the added, that is updating.
17:15Now, supposing there is an emergency that has arisen.
17:20Let's say maybe network outage.
17:22And this is something you need to queue it immediately so that it can be fixed immediately.
17:32So, you can just say tasks.append left agent.
17:43Fix network.
17:57So, something agent authorised, that is an issue with network.
18:02You just append it on the left.
18:04So, let's say execution starts from the first item.
18:09When you append this on the leftmost side, and then automatically on the left.
18:18So, if you run this, you can see now agent fixed network, that is the immediate task.
18:30Then, supposing it has been completed, because it was agent, you can say the task.pop left.
18:46Then, I can say print network fixed.
18:57Something like that.
18:58Network fixed.
19:03Network fixed.
19:05Then, from there, I can display the full list now.
19:13So, we come back, display the full list, print, send email, update.
19:19So, you see agent, written wrong spelling here.
19:26There must be an end there.
19:28So, you see that network fixing was agent and was dealt with immediately, before the rest
19:40were looked at.
19:43What is it about DEC method?
19:48The DEC module, it's a function.
19:53It is found inside collections module, and that is what it can do.
19:59So, we learn about it, how to create DEC, how to import DEC function, so that you can use
20:09it.
20:10Basically, this is a function call, and what I'm supplying inside this function is a list
20:15of items.
20:21So, this is it.
20:22If you have any questions, you can always reach out.
20:26If you have any questions, you can always reach out.
20:27Anything to do with Python programming, cybersecurity, and general IT, you can always reach out.
20:36So, until next time, when we will be talking about chain map.
20:45So, today we will be looking about DEC.
20:51Tomorrow, we will be looking about, we will be talking about chain map.
20:56Today we have discussed about DEC.
21:00That is the double-ended queue.
21:02So, queue next, and watch out for the rest of the collections modules this time.

Recommended