ALL 11 LIST METHODS IN PYTHON EXPLAINED

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to be covering every single method that comes with lists in Python so just to explain it real quick we have 11 that we are going to be covering such as append clear extend index all the way to sort and this function is not including the dunder methods so if we just remove done the methods you'll notice that there's a lot more that comes with list but we will keep this video sweet simple and informative so we will start with the basic 11 and what I mean by list methods is suppose you have a list as soon as you use dot notation you're going to get some context options and they're going to be the methods that belong to the list type it appears we are being invaded by helicopters and I need to wait for the helicopter the first method is append we have a list of people here and we want to add a person the best way to add a person here is to append a person so we can type in people.append and Luigi can join the party and if we print the people we will have Luigi appended to this list and I'm going to put this down a bit next we have clear and clear just empties the list completely so now if we want to print this list of people we will have no one inside the list because we cleared the list number three suppose you want to copy this list now to do that we'll type in copy people for example of list of type string and here we'll just type in people dot copy so here we performed a shallow copy which means we can edit one-dimensional arrays without any side effects so now we can type in copy people dot remove and we can remove Trump for example and if we print both people and print copy people we will have two separate lists with two separate IDs these are their own objects but as I mentioned in a previous video If you have a list of multiple Dimensions such as a list of string inside a list of shrink so here we will just type in Elon and Bob his favorite employee if we try to change this element it's going to change both of them for both people and copy people so here we'll type in copy people at the index of one at the index of one is going to equal cat so this should replace Bob only in copy people in theory because of course we copied that array but if we run the script it's going to replace it in both lists but if you're curious about what happened there I'm going to leave a link in the description to a video that I made regarding that for number four we have counts and for this example I had to duplicate Elon in the list otherwise it wouldn't make sense but here we can now type in people dot count and and we can select a value that we want to count the number of occurrences in the list for such as Elon and we should assign this to its very own variable called elon's and we're going to print elon's so here we have two elons in the list and that's what count is doing it's counting the occurrences of an object inside an array so we can do it also with Mario and we only have one Mario there and if we type in something that doesn't exist such as Apple we will have zero as a return next we have extend so suppose we have another list or another iterable that we want to append to this list with extend we can do that quite easily but we need to create a list first so people two of list of type string is going to equal apple and banana then we will just append to people or actually extend the people with this list so extend that list with people too and we will print the original list of people and when we run it we will have that list inside this list if you want to put a list inside a list just add a list and it's going to put that list inside that list so you can put anything that's iterable inside extend and it will add it to the list now suppose you want to find out the location of a certain element in a list python has a method called index to do just that so if we are searching for Trump you can just type in Trump here and if you run it nothing's going to happen because it's supposed to return to us a value and we will print that value so we will find out that Trump is at the index of two so that can be quite beneficial in a large list now if you type in something that doesn't exist such as Luigi we're going to get an error so you need to make sure you handle this properly and try to find elements that actually exist in your list suppose you need to insert an element at a specific index you can do that using the insert keyword so people dot insert and here we need to put a value and we can say we want to put Luigi next to Mario so we're going to insert Luigi at the index of one so here we'll say Luigi one and this didn't work because I used index instead of inserts and looks like I'm still a noob because the position comes first and then Luigi and now if we print this list of people we will have Luigi right next to Mario because that's where we wanted to insert Luigi we wanted to make sure Luigi was at the index of one now if we try to insert Luigi at the index of 5 it's going to insert it at the end of the list so it doesn't matter how far you go up you can put 100 000 if you want or that's ten thousand now one hundred thousand if you want and Luigi will still be appended to the end and you can also do minus 1000 and Luigi will be appended to the front so it does its best to find the location you want to put it and if that location makes sense it will place it right there for number eight suppose you want to remove an element from the array and return it one way to do this would be to call people dot pop so if you call it by default and print the people it's going to pop the last person from the list which is Trump but you can also specify an index so if you want to pop Mario you can type in pop Mario at the index of zero and what's nice about pop is that it returns a value so we can say popped it's going to equal people.pop so we can type in print people and print popped and if we run it we'll have Mario that was popped from it so you can do some sort of further processing if you need to do that very similar to pop we have remove and it's actually very straightforward you type in people.remove and you provide the value that you want to remove this time we don't want Elon so we will print people after having removed Elon and he will no longer be part of the list and if we try to remove someone that doesn't exist such as Luigi we're going to get a value error that that value cannot be removed because it doesn't exist next we have reverse which does exactly what it says so if we have some people we can say people dot reverse and then we can print these people and if we print the people it's just going to reverse that list and as you saw there it was just that simple finally we have sort which allows us to sort our list According to some sort of key and we don't need to provide a key originally it's going to do it alphabetically so we can type in people.sort and print the people and by doing that we will have the people in alphabetical order although it's important to note that if you have letters that are lowercase and uppercase it's going to start with the uppercase and then do the lowercase so sometimes it might be a good idea to provide a key so for example we can type in key and here we can provide a Lambda function or you can just create a normal function if you want so the parameter is going to be the name and we're just going to return the name dot lower this way even if Bob is uppercase or lowercase or Trump is uppercase or lowercase if we print these people they're going to be returned in alphabetical order regardless of their case or you can also create some sort of key that Returns the people depending on the length of their name so here we start with Bob Elon Mario Trump and Luigi and this is going to be in alphabetical order since they all match at five and as with any sort function you can also reverse it by setting reverse to true and if you run that we'll have the reverse of what we just did and to keep it simple we can also get rid of the key and just run reverse and it's going to work exactly the same way we're going to start with Trump Bob Mario Luigi and Elon after having sorted the list but those were the 11 list methods in Python explained do let me know what you think about them in the comment section down below whether there's some cool trick a tip you know about it which I did not explain in the video I would love to read that but with that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 79,054
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: 0yySumZTxJ0
Channel Id: undefined
Length: 9min 23sec (563 seconds)
Published: Mon Jan 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.