Hello, everyone, in this particular session, we will discuss about list, we will see how to create
a list, how to retrieve elements of a list, how to add different objects to a list, how to insert
element at a particular position, and how to remove objects from a list.
We would also see how to sort the list now.
First, we begin with the list.
Let us discuss about the different characteristics of a list.
So the first thing about list is that list is ordered in nature.
Ordered in nature means that we will be able to retrieve a particular element from a particular index.
Hence it is ordered in nature.
Next is mutability.
Now what does mutability?
Mutability is a concept where a particular database allows us to change the element itself.
It allows us to change a particular value.
So if we talk about strings, strings are immutable in nature.
How can we see that?
So let's say we have a string as I try to modify a particular character.
Is same at index for I want to put.
Let's I want to put a character B, let's say I want to put the character so.
We get the error that string object does not support an assignment.
So this means that string cannot be modified, a particular index of string cannot be modified, although
if you try to do something like.
S is equal to be something, something so we will be able to do that not just because we are trying
to replace the entire object.
So here we are creating a new object.
It is an entirely new strain being assigned to the variable.
So this is possible.
But when we try to modify a particular index of a string that is not allowed because string in itself
is immutable in nature.
But when we talk about list list as movable in nature, so because of that we will be able to modify
a list.
We will see how we can do it.
But after some.
Now let's see how it supports multiple data.
So in case of a list, we will be able to add different type of data types.
So you would have seen that in some languages it is allowed to add only one data that you can have a
string or you can have a number.
But in case of list invited, we will be able to add any mixture of different types.
So let's first of all see how do we create the list.
So first of all, we will have to give a variable.
My name is Enlistee one and I want to add certain elements to it.
So let the elements be string a B next element.
Methe next element be one, then two, then three.
Let's have another element that's true.
And another element does fault, so you can see I can have different combination of values in the list.
Now this is my list and just my list for you.
So this is my list.
Now, in case you want to retrieve a particular element from a list, we can do it by say.
LSD one and I want to retrieve the zeroth index.
We can do it by Elstein one zero.
Let's say we want to retrieve the last element so I can do it by using minus one.
Sorry, I don't have anybody, I have it is the one, so if I do a listed one, I am able to retrieve
the last element, which is minus one.
Now, let's say we want to retrieve NSD one.
We want to retrieve the this particular element, which would be minus one, minus two, minus three.
We want to retrieve the negative third element and it comes out to be three.
Now, let's say we don't want to retrieve just one element.
We want to retrieve multiple elements.
How would we do that?
We do that by saying the last one.
I get that all the time and see one.
And after that we say from zero the next to now, we also need to see how we can find out the entire
length of a list so we can use then.
And we can give amnesty one inside this, so this gives me the entire length of the middle of my list
of seven so I can go up to six.
So let's say six so I can see all the elements.
And remember, in slicing, what happens is this is the starting point.
And when I say six, this means that I want to retrieve element from zero to five.
Six means that six is not supposed to be included.
If you want to get all the elements, you just put nothing and retrieve all the elements.
And if you give seven year, then it would give all the elements.
So what you put here is the last element which you want to retrieve plus one.
Always remember that this last element which you want to retrieve plus one.
Now, the next thing which we can do is let's say if you want to retrieve the elements ultimately and
we want to skip one element in between piquancy to this means that we want to skip one element in between.
So if I retrieve this, I get the alternate values.
Now, the next thing which I can do in is let's say if I want to add different objects to a list and
let's say you want to combine different list.
So let's create another list, say let's do it on my list or I will put in some numeric elements.
One, two, three, four, five, six.
And if I want to join these two list, I can simply say enlistee is equal to L'Estang one plus Amnesty
two.
And now if I print my list, I will give get a list, which has all the elements of list one and two.
And if you see these have all the elements from list one list.
What else I can do is I can also use dot up and now when they use dot up and this means that you want
to add values, which are a single line, you now dot up append, always add whatever you given as an
argument in just one single index, it will be added to the next index, which we have.
So here, let us see the next index, let the same list one and see this one.
And if you see this, do you see the length of to.
So the length of two is six, so this means that we have values from zero to five already present,
but there is no value on the index.
Seven and six, so let's say let's do an index six, so you will see that there is no value present
here.
Now, if I want to add certain value, what I can do is I can simply saying this to not offend.
And inside this, I won't give any object which I want to I.
So let's say I want to add.
Under number eighty nine.
Eighty nine.
And let me bring up the list to after this.
So you can see the element which we added has been under that nasty mix, now, let us try one more
thing.
Let us try to append another list.
Let us see.
We tried to open the list.
Of elements, eight gunmen, nine gunmen then, so now what will happen is it upend the list entirely
in the next index, so it does not believe, I believe, over the elements and add them in two different
indexes.
It just picks out the object and puts it in the mixed index, which is available.
Now, in case we don't want this thing to happen, then what we can do is.
We can simply use.
Extend.
What extend does is let's say we want to have 11, 12.
13 and 14, so what extend would do is it will I trade over this entire list and it will add the element
at the end of the list and it will add them into different indexes.
So let's do that.
So now you see these elements were added.
And one thing, the index like 11, 12, 13, 14, they have been added in two different indexes.
It actually I traded on top of the list, which we provided and added the values and different elements.
Now, let's try to do one more thing, let us try to instead of having this entire thing for discussion,
let us try not.
So let us try out a string.
Try to it.
OK, let me copy this.
And word this fact, what we want to be had, so let me add more cells here.
So now let us try this.
So it added these elements in two different indexes because DLI is a string.
And as we know, the string is a collection of characters.
So it added I drifted over the string and added these characters one after the other one.
The next thing which we can do is we can add elements of the political opposition using bought and sold.
So let's say we have a list to, again, see a list to do, and we want to do it in sort.
And we want to insert an element, our position C three.
And the object, which we want to insert is new.
And let's bring in as to do so.
This is the new list.
The new object has been added of the index three and how we verify index three.
This is zero.
One, two, three.
So add the third index.
This new object has been added.
Now, that is another thing.
Let us try to remove objects from a particular list.
So to improve in to remove objects from a list, we can do enlistee, do dot remove.
Now let us try to remove the element three from it, so let's try to remove three.
So we will simply give three here and then print.
And unless to do so, the element three has been removed from this particular list.
Now another thing which we can do is we can do something we can use and see pop.
So ListBox will remove the last index by default, so if I do something like.
Doubtful it will remove the last element for us and it would bring down the value to us, it will return
the value to us.
So let us print.
While you and Leather's rent.
And so once we run this, it has blocked the volume from the last index, which was which was why?
Because we have not given any argument.
We have not given any particular index.
So by default, it assumes that we want to pull from the last index.
So it removes the last index from it.
And the president does and the list is updated with removal of the last element.
Now, apart from this, we can also do one more thing.
We can do another.
Index so we can provide another index save for so let's say we want to pop out index for some before
popping out index for.
So let's try to bring this one.
So when we run this, the object from the fourth index has been removed.
So zero, one, two, three, four.
So the fourth index has value five index, which has been removed and the list has been updated without
the.
Index five.
Now, the next thing which we can do is we can sort the list to sort of list, we can do something like
LSD to.
Don't start now when we started this particular list.
Notice that we have numbers also, we have got those also and we have a list also.
So it is a mix of different type of elements.
So now if I try to sort the list, it gives an error because it cannot really compare these different
types of lists.
So let us try with another list, a simple list.
So let us try this.
And that is great on the list now.
So let's say list for and list four letters have values like one five three seven nine and forty four
and nine nine different lists and us sort this list.
So once we saw this and bring the list.
Sorry, list for.
You can see them, this has been sorted, another thing which we can do is we can you sorted.
Missed and we have option to reverse this also so I can give.
List for it, and it can sort of in reverse order and give us a list, so these are different kind of
methods and functions which we have list which we can use.
This is what we had for this particular session.
I hope you understood this in the next session, we would go through the loops and conditions.
Thank you.