Intermediate Python Tutorial #8 - Collections/Deque(deck)

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·7y ago

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 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →