Intermediate Python Tutorial #8 - Collections/Deque(deck)
Skills:
Python for Data90%
Key Takeaways
Intermediate Python tutorial on using the deque data type from the collections module for efficient element addition and removal
Full Transcript
hey guys and welcome back to the AP do and my intermediate Python tutorials in today's video I'm gonna be continuing with the collections module and we're gonna be talking about a data type known as the deck so yes this says DQ right here and the title of the thing is spelt DQ but the way that you pronounce it is Dec and I just found that from reading the documentation otherwise I was gonna go through this whole video saying DQ anyways Dec is the proper way to say this word so if I do end up saying DQ throughout the video please don't leave an angry comment about it I don't know why I just keep saying it but anyways Dec is the proper way so why would we want to use a deck over a typical list so pretty much it kind of looks the same as a list and I'll show you in just a second but the reason why we'd use a deck is because it's faster in terms of adding elements to the end and the beginning of a list so rather than using a list you want to use a deck because it's gonna be faster whereas if you're trying to randomly access elements within a container so a list then you're gonna want to use a list over a deck and if that's confusing don't worry I'll kind of show you how we as we go through so let's start by creating a deck and see how that works so I'm just going to say D and it's gonna be stand for our deck is gonna be equal to a deck and then this just takes an iterable argument so in this case I'm gonna take type in hello but I could give it something like a list a dictionary so on so anyways D equals deck hello now if I run the program press f5 and I simply print D to the screen here you can see that we get deck and then h-e-l-l-o I just split that up into what looks like a list but it's actually a deck okay so now let's go through a few methods that we can use on this so obviously we can use something like append so we can append an element to the end so I can append something like for I can depend with other elements do something like 5 and if we run this and then print it again you can oops didn't mean to do that then you can see we get hello and then 4 or 5 so on so you can append a nice thing is you can actually append to the beginning of the list as well so I can say append left instead of just regular append and now you can see when we run the program and we print D don't know why that didn't work let's see here we get five and then hello four so that's a nice method as well we can do the same thing with popping which is removing elements from the deck so I can do pop in this case and I'm not going to do four in here so a new deed on pop and then I can also do d dot pop left and what pop left is gonna do is it's simply gonna remove the first element in our deck rather than the last so pop this works on list as well and dictionaries and stuff you can remove the last element or whatever index you type in here so like five to whenever that's the one that's gonna remove pop left simply removes the first one so I'll do this and then print D and you can see that we are left with E ll because we removed oh and we've removed the H okay another method that we can use is clear this one is simply going to remove everything from the deck so that's pretty standard print D again then you can see that we have an empty deck now okay the next few that I'm going to talk about here I believe you can use on some other containers in Python but they do work on this deck as well so let's go through that so now that we already cleared our deck let's just go ahead and add some more things to it so what we can do is we can do something called extend and what extend is gonna do is it takes an iterable argument which means anything that's a container is like a list string something like that and it's gonna put it at the end of our list so if I have something like four or five six and I run the program and I print D you can see we get four or five six at the end of our list now to show you what happens if you already have something in I called it a list but our deck is if I extend again I'll extend something like so we can see it better below then what we get is print D we get four or five six and then hello so this is a weight that to add multiple things into your deck so again if I make like a list and I got like one two three this is gonna work as well so we should get four five six one two three it's current D and there we go we get that as well so if you ever don't want to run through like a four loop to add an entire list into a deck then you can simply use extend extend also works really nicely with extend left which is gonna add to the beginning of our list so now we have a list of four five six one two three let's see what happens if we extend the word hay to the left side of the list so I'm gonna print that and you can see we get yeh four five six one two three you might say well why didn't we just get a CH well it's because we're actually extending to the left so we're gonna add this in the reverse order and that should make sense because if you're extending this first and then you extend this and then you extend this why should be the first element on the left side of our deck like that okay so now we're gonna use a few other methods so this one is probably the most useful one that the deck has and the reason why you would use it if especially if you're gonna be dealing with things at the beginning in the end specifically and it is known as rotating so what this does is it takes a integer in here either positive or negative if you put a positive integer in here it's gonna rotate all of the elements by that amount to the right otherwise it's going to if you put a negative element in there rotate everything to the left so just see what happens if I put a negative one in here to rotate to the left and then we'll print down here certain side the bottom of the screen right now and you can see that we went from this one which was the last D or whatever when I printed it and now we've rotated everything over so threes moved over to the position of two and when we get all the way over to here we've switched E and Y so E's moved over here and Y is been rotated back to the end of the list now you can actually rotate with other numbers will also rotate no by two and then you can see if I print D down here that we get it rotated two elements over now and this is really useful when you're trying to solve certain problems to be able to use this rotate again you can do the same thing with positive so if I just rotate it by one now and I simply Printy you can see that now we've rotated to the right side so three which was originally here has now been moved up to the beginning of the list okay so now we've talked about that there's one last really useful thing that I left out on purpose because I wanted to show now that we can do with these decks so when we initiate our deck here we give it an interval object we don't have to give it anything it'll create a blank one if we do that but there's something it has called Max Len now if I do something like Max Ilan five and I'm just gonna remove all this and I'll show you how this works so I'm a max Alana five and I have a string that has five characters in it if I try to add something to my deck here so I'll just do I'll just simply I'm gonna print it here just so it saves us some time get a print D and then I'm gonna go D dot append let's do one and then print D again let's just watch what happens so you can see we get we have a max on a five we start with hello and then when we add one H is actually removed from our deck now the reason that happens is because when we set this max length when we start adding things into the list it needs to remove something to maintain that attribute of a maximum length of five so it removes the first element from our deck and that's really useful as well if you're solving problems because then you don't have to deal with removing things every time you add something to the end or removing like a slice of for example list if you're gonna be adding with the one thing so if I extend like something like let's see here one two three now we should see that H E and L should all be removed a mission at L o one two three so let's try it and there we go we get hello and we get L one two three and we didn't have to deal with figuring out how many elements we need to take from the left side to the right side because of this max Len now just to note you can't change this max Len by doing something like D dot max line equals five its artwork Isis will do like six because it's already five you can only access what the max line is by just doing is simply this print MACD dot max line and this should just give us five and it does but if I try to do but I was showing you there so d dot max line equals five you'll see the error that comes up and it says this attribute of Max Len is not writable so we can't actually change it after we've initiated that value so anyways that's pretty much been it for Dec within collections this is really useful and you might not say useful now if you start solving some more advanced problems using this deck will make your life a lot easier with those methods that I went through and I'm gonna copy all of them down alright it's like a little paste bin thing so you can click the link in the description there if you want to see all of them in a list so you don't have to specifically memorize that other than that you can always just go to the documentation and have a look it we just search up like DQ collections Python it goes through what all of these to do again I forgot to talk about reverse but it's pretty straightforward you can use reverse and if you're followed up to here you probably probably already know that one so anyway so that's been it for this video if you guys enjoyed the video please make sure you leave a like and subscribe I will see you again in the next one [Music]
Original Description
Intermediate Python Tutorial #8 - Collections/Deque(deck)
Today's Topic: deque, this is a unique data type within the collections module in python. It is much faster than a list in respect to removing and adding elements to either side of it. It has many cool and useful methods which I showcase in this video.
In this set of videos I will be explaining more advanced programming concepts and showing you intermediate-advanced tools that you can use in python. A lot of these tools will help you to solve problems/code more efficiently and will save you a ton of time!
Text-Based Tutorial: https://techwithtim.net/tutorials/python-programming/intermediate-python-tutorials/collections-dequedeck/
Twitter: https://twitter.com/TechWithTimm
Future topics:
9. Collections: orderedDict
10. Collections: defualtDict
11. docstrings
Want To Support This Channel?
Bitcoin: 1PbkAYLFaJBgjbKn2ptGyBz65xWN8hJgBU
Ethereum: 0xdd42dbbdba60f7163fc7a840e189474b6e8bfcad
Ripple: rD4arM9CVjQWqi8f1kxdpCgkCgEkqBgtud
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Python Tutorials
- Intermediate Python Tutorials
- Collections module python
- More advanced python topics
- deque python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Python for Data
View skill →
🎓
Tutor Explanation
DeepCamp AI