Python Course in Delhi | Advanced List Guide in Pythonby Manoj Singh rathore Digital Marketing Head and Founder
Hello, everyone, and welcome to the Advanced Lists python course.
So, in this series of python course in delhi, we're going to be diving a little deeper into all the methods available in a list.
So, these aren't officially advanced features, just methods that you wouldn't typically encounter without some additional exploring.
So, we've actually seen quite a few of these, append, count, extend, index, insert, pop, remove, reverse, sort, so we may have seen a lot of these already just by using lists and in the Methods python itself, but let's go ahead and go over them just in case you need a refresher.
Okay, so let's start by making a straightforward list. I'll call it l, and I'll have one, two, three in it. If you have any trouble in creating list in python. You can join our python course in Techstack.
You definitely should've used this method by now. It appends, whoops.
It's the append method, and of course, it just appends an item to your list.
All right, so we discussed this also several times, the count method.
So, we can use the count method, and it'll count how many times an object occurs in your list. Moreover, it'll count whether it's in there or not,
so notice how I count a 10, doesn't occur many times.
One occurs once in my list. So, moving along here's one that you may not have heard of, and, or may not have used yet.
It's called extend. So, many times people find the difference between extending and append to be unclear.
So, I'm going to show you using an example.
So, let's go ahead and make a list called x. It's likey, it's, er just like the l list, it's going to have one, two, three. I'm going to append another list to that.
Moreover, this list will contain four, five. Moreover, then, I'm going to say print x.
Okay, so what happened here? So, remember to append adds the entire element to the list, so what's it's doing here is
it's adding this and appending this list to an element in itself.
Now, what happens if you wanted to extend this list into your original list?
So, that's where extend comes in. So, now I'm going to show you again,
However, at this time we're going to use extend. So, I'm going to say x.extend and pass in a list again with just two elements, four, five.
Then, I'm going to say print x.
So, notice the difference here.
You're adding in the entire list. So, your extend will append each element in that iterable.
Moreover, that's the key difference there.
So, again, to cover append is going to append elements just the entire iterable, all the elements in it.
Moreover, what extent does is appends elements individually from the iterable.
All right, so let's move along. Take a look at our l list that has one, two, three in it, or one, two, three, four, excuse me.
Moreover, the index is another method that we've probably seen already.
So, what does the index do? The index will return the index of whatever element is placed as an argument.
So, let's find the index of two. Moreover, it's one.
Because, yeah, indexing starts at zero, zero, one, the object at the one index is two.
Something to note here is that you will get an error if you try to index something that's not in your list.
Moreover, it'll tell you very clearly value error 10 is not in the list.
All right, let's move along with insert. So, what does insert do?
Insert takes in two arguments. It takes an the index and the object.
So, this method places the object at the index applied.
So, for example, let's make sure we know what l looks like.
L looks like one, two, three, four. Moreover, what I'm going to do is in that list I'm going to insert at index two a string called inserted.
So, note the order here of the arguments. You put in the index first and then the object you want to insert.
So, what does my list look like now? Notice how it has the inserted at the two indexes.
So, zero indexes, one index, and at the two indexes, it's placed this string inserted here.
So, again, you put your index in first and then the object you want to insert second.
Okay, might be useful if throughout your code you want to make sure that you know the particular index of an object.
Okay, moving along, you've already seen pop, which allows you to pop off the last element of a list.
So, again, here's the list, and I can pop.
Moreover, by default, it's the last element in that list. So, I set l pop equal to ele, and ele, you notice it was four. Moreover, again, remember that change is permanent.
It's in place on your list.
So, something else to note is that you can pass in an index into pop.
So, I could say pop zero, notice how it's one, so now l looks like this, two, inserted, three.
So, what's another method for a list that we can use? Remove is another method.
So, the remove method removes the first occurrence of a value.
So, for example, I have my list, and let's say I want to remove that string inserted.
So, I pass in the value, and it will now remove it in place.
So, I want to show you one more example with this, to make sure you get the idea that it's only removing the first instance of it.
So, here you notice my list is one, two, three, four, three. There are two threes in it.
So, if I say l, remove it's only going to remove the first instance of it.
So, that's something to keep in mind with remove.
Another method we can use if I seel here is reverse, and as you might guess, reverse reverses your list and reverses the indexes for them.
So, note that this occurs in place so that it will affect your list permanently.
Moreover, we can also use a sort method on our list.
Moreover, the sort will sort your list also in place.
So, those are all the methods on a list.
Some of them we've covered a lot.
Some of them might be new to you. However, go ahead and again review the Jupyter notebook. Make sure you understand everything.
So, let's go a quick rundown of everything we learned.
So, append, you've used this method.
It appends an element to the end of a list.
Count, we've discussed this as well.
It'll count how many times that object takes place in your list.
Whether or not it occurs in your list
it won't return an error.
Extend, here's the tricky one.
Remember in extend appends each element in that iterable, so it appends here four and five, not just the list with four, five. The index will return the index of whatever element's placed in as an argument.
However, remember, it has to be on the list. Otherwise, you get an error.
Insert takes in two arguments, first the index and then the object.
So, here we insert it at two, this word or string inserted.
Moreover, notice how it's inserted in the set index two. Pop, we've seen that already, allows us to pop off the last element of a list, or we can also pass in a specific index if necessary.
Remove, the remove method removes the first occurrence of a value, so we just saw an example of that.
Reverse, as you know reverses a list, and sort will sort your list in place.
So, both reverse and sort are in place and will affect your list permanently.
Okay, that's it for the Advanced Lists python course, which is just all the methods on a list, and some of them you wouldn't typically encounter without some additional exploring.
Okay, thanks to everyone, and I'll see you at the next python course.
Created on Apr 20th 2019 14:40. Viewed 477 times.