What I use to learn (the BEST IT training): https://ntck.co/itprotv (30% off FOREVER) *affiliate link
FREE Python lab: https://ntck.co/pyep9
In this episode of Python RIGHT NOW! Chuck is going to show us how to lighten the load on our Python camping list(we packed too much). You will learn how to use the Python methods clear, remove, and the best of all, pop! These are the ways we can DESTROY or….just delete….our Python Data in Python Lists.
🔥🔥Get access to all of my courses plus more by joining the NetworkChuck Academy!: https://ntck.co/NCAcademy
**Sponsored by ITProTV
0:00 ⏩ Intro
0:43 ⏩ I want to show you something!
1:55 ⏩ Back to our camping list!
2:15 ⏩ We packed too much!
3:10 ⏩ Don’t do this to your list!
4:25 ⏩ The best way to remove items!
6:11 ⏩ Pop! Pop! Pop!……. Pop!
8:53 ⏩ Outro
Warning! We are about to use Python to destroy, obliterate data. Please try this at home, in your lab, like not in production. No, yeah, I’m being a bit dramatic, big surprise, but seriously, in this video, we’re gonna level up once more, our Python skills, more specifically lists, and we’re going to learn how to obliterate, destroy Python data but really we’re just removing stuff from a list. So, just that. But it’s still pretty cool and a crazy valuable skill. Quick coffee break before we start. And if you haven’t already, check that link below to launch our free Python lab for this episode. Seriously, it’s a free Python lab right here in your browser. Go click it, like now. It’s free. And while it’s loading, can I show you something I’ve been working on? Check this out. Here in the Hands On Python for Networking Professionals course on IT Pro TV. I am learning how to build my own subnet calculator because all of us kind of suck at subnetting. See that series right here. But if you suck less at Python, you can become awesome at subnetting by building your own subnet calculator in Python. How cool is this? Ah, and speaking of IT Pro TV, they are the sponsor of this entire series and the reason it can be free here on YouTube. And they are my choice for IT training. They are what I use to learn stuff obviously. And pretty much whatever you wanna learn in IT, they have you covered from just getting started with the A Plus to their latest courses, like Windows Terminal Skills, PowerShell Skills, Hands-on Web App Testing. They’re constantly adding stuff to their library and that’s why I continue to watch them to learn new stuff. And there’s an added feature to where you don’t fall asleep when you watch their videos, which I think is pretty cool. So anyways, check them out. They’ve got virtual labs, they’ve got practice tests. They’ve got everything you need to become awesome in IT. Did that rhyme? Kind of, a little bit. I’m gonna go with it. So check them out, link below. ITpro.tv/networkChuck. And if you use the code networkChuck, you get 30% off, forever. So in case you’re new here, you and me, we’re going camping. You’re coming, I’m making you come. And in the last few videos we’ve been putting together our camping supplies list, and you almost got us in trouble, because we almost forgot, and when I say we, I mean you, toilet paper and for me, a bidet. But thankfully we were able to add those supplies with some methods, but we got a bit overzealous. We packed too much. You see I’m taking my Tesla because it’s a Tesla and we can’t fit everything inside. So, now we have to remove things from our list. Now I know what you’re probably thinking. Well, Chuck removing things from a list is pretty easy. Just go in here and start deleting. No, yeah, I can’t do that. That’s lazy. We’re gonna do it programmatically with Python and skills. Okay? Okay? Sorry. I feel like very aggressive in this video. Must be all the coffee. It’s okay. It is one of the side effects. So here’s our task. We have to remove some non-essential items from our list. Now we could remove the raspberry pi or ethernet cable but no, too essential so we can take away our tent and sleeping bags. Yes, that’s what we’ll do. So, remove tent and sleeping bags from our list with Python. Now, just like we used a method to add stuff to our list. We’ll also use a method to remove things from our list. So let’s try this out. The code we have right here. Go ahead and run it and sure it works. Cool. All our supplies are right here in the console. Now just after we added toilet paper and bidet, let’s go and do this, type this with me. Supplies, our supplies list. And then do dot clear the method clear and then parentheses. Oh not nine, parentheses. Easy enough right? Let’s run the code. What did you do? The data, it’s not there. It’s gone. I told you just to delete two things and now the whole thing’s gone. It’s okay. I know it was a bit nuclear but I wanted to show you this one first. It’s just more fun. So, if you wanna take out your entire list just for some reason, you can use the clear option, the clear method, and it will do the job. So clear is nuclear and notice up here, it actually printed a list with nothing inside which actually brings up a really cool point. You can have empty lists, which seems pointless, right? But no, but it does come in handy when you’re programmatically adding things to a list but you don’t have anything for that list to have in it yet. That was a really awkward way of saying that. But essentially what you might end up doing is defining a list like this and that sucker’s empty. And then later on in your script when you’re doing something really cool, which we will do later. I can’t show you it yet but you’ll start adding stuff to your list using those methods we talked about. So yes, lists can be empty. Little fun fact for you. So anyways, clear is cool but we may not want to eliminate everything. So let’s take that out. A bit overzealous today, aren’t we? So, now let’s try the most intuitive method out there. If you wanna remove something from your list, you might want to use the remove method and that’s what we’re gonna do, remove. So dot remove the color changes, things change. I love it. Parentheses and then here, how do we specify what piece of data to remove? We just simply call out that value. So for example, removing our tent and sleeping bags. So let’s try tent, that’s it? Then click run. It’s gone, no tent here. Now one key thing to note with the remove method is that when we wanna remove something, we specify the actual value, the actual name of the data. And obviously it has to be correct. ‘Cause if you put instead put tents, we’re gonna get an error ’cause tents aren’t there. It’s just tent. So real quick, why don’t you try and remove sleeping bags. Go ahead and add that code for us so I don’t have to do it. Pause the video. Unpause. Okay, coffee break. Here’s how I would do it. We’ll do another remove method here. Supplies dot remove and same story as earlier, parentheses, specify our data which will be sleeping bags. Just how it looks up there. Now, if we run our code, bam, tent and sleeping bags are gone. Hey, you know, we kind of just already did it. Our Tesla is lighter. We can get there. It’s fine. Now notice this. We can only remove one piece of data with our remove method which is important to remember, ’cause you may have tried this. You may have tried to go, hey, if I can just put a comma there and put in tent, it might work, but it won’t, you’ll get an error. Watch, error. Now how to remove multiple items from a list is not the simplest thing to do. And it’ll be a concept to cover later on on this course. Now you probably assume this by now but there’s more than one way to remove an item from a list. Yeah, we have a remove method and he’s pretty great. And instead of using the value to remove stuff, we’ll use the index, which is very handy and you know this new method also has a cool feature that you probably don’t care about but you’ll care about it when I show you. Okay, I, let me just show it to you, okay? So let’s remove our remove methods real quick. Just take ’em all out. Obliterate. Same story as before. Let’s type our supplies list dot and then here comes the fun one type in pop, pop, pop, pop, pop. That’s what it is. That’s the method, it’s called pop. That’s kind of a fun one, right? And then parentheses. And now I mentioned, yes, we’re gonna use an index to remove our data instead of the value. So, I want you to do this by yourself. Knowing how indexes work and how pop probably works, try and remove our tent and sleeping bags from the list. Pause the video, go. Unpause, coffee break. With my first pop method statement here, whatever, I’m gonna remove tent. And I know the index of tent is going to be the zero. Cool. That should do it. Actually, let’s run the code and make sure it works. Tent is gone. Awesome. I’m gonna do the same thing down here. Supplies dot pop. And I’ll remove and actually this is kind of interesting because I didn’t realize this as I was doing it. I’m gonna do zero here. What? Watch, watch. I bet you it is gonna work. Run, totally worked. So tent and sleeping bags are gone but why did you use zero twice? Well, because when I removed pop, the index changed, right? That required a bit of critical thinking, didn’t it? It was kind of fun though it was like a puzzle. So the first pop method, we removed tent. So the zero index tent obliterated. Now, after that happened, sleeping bags was in the zero index and we’re like, okay, now we wanna take out sleeping bags. It’s not gonna be one like you might have thought, it’s gonna be zero. That’s kind of fun. That’s kinda a little fun puzzle. If you got that right, let me know. And also keep in mind how your indexes will change if you use multiple indexes, indices, how do you say that? I don’t know. Now pop is pretty cool because of the way it removes stuff by indexes, but it does something else cool. It has a little hidden feature. When you remove something with pop it not only just, you know, takes it out, it will also return that same data. What does that mean? Let’s try it out. Let’s just demonstrate that sucker. What we’re gonna do real quick is actually just surround that supplies dot pop zero with a print statement. Let’s do that. So we’ll print open parenthesis and then closing parenthesis over here at the end. So, now we’re gonna print the return of this program here, this little script. Let’s try it out, run. Did you see that? Look at that up here before we printed the entire list it just printed tent because the pop method will return that data that we’re deleting, which is kind of cool. It’s like a combo move. Bam, delete, bam, let me show you what I just deleted. One, two punch, I love it. Now you might be thinking, well, what’s the point of that? That seems pointless. Well, you might want to, you know just have a confirmation of something you delete, right? That’s handy. So, for example, I might do this. I’ll print this item was just deleted and then I’ll add that string I just made to the return of that pop method. So now if I run the code, run. I get this nice little output about something I just did. This item was just deleted. It was tent and it was also just deleted. Like that’s cool. One, two punch. Take yourself a quick coffee break ’cause you just obliterated some data. We dropped a nuclear bomb with the clear method, just taking out the entire list. We then removed one item at a time with the remove method but we gotta specify the value and then we popped one out using the index. Pop! Which will also return the data that we just deleted, which is kind of handy. Now there’s something weird, something that’s just like a list, almost identical to it, but it’s faster. It’s almost better in every way as far as running programs, it’s something called a tuple. What? Sounds weird. Now, if you don’t wanna wait, I already have this next episode ready to go locked and loaded on Network Chuck Academy. And if you wanna get some extra practice with all the stuff we just learned, because you do need that practice to cement that knowledge into your brain, it’s all about repetition. If you don’t use it, you’ll lose it. So check on Network Chuck Academy, where I’ve got some extra quizzes and labs to get yourself practiced up in Python. I can’t talk anymore. Oh, and also have you hacked the YouTube algorithm today? Let’s make sure you do. Hit that like button, notification bell, comment, subscribe. That’s it, everything, yeah. That’s right, because you gotta hack YouTube today. Ethically of course. Yeah, that’s pretty much all I have. I’ll catch you guys next time.