[no audio]
Friends, here we are going to discuss the very first
data structure of your Python, that is list data structure.
So first let me open my Notepad and I will show you how
to define a list.
So guys any name you can take. Simply
I am taking 'my_list='
this one, this is an empty list.
So if you want to create any list with some values, right
let me take 'my_list1=', so
[no audio]
you can provide any type of data.
Right. Now, so this is the way how you can define your list.
And guys, the very first thing in your real time,
I mean very important point in your real time from list is
sometimes you need to convert your list into Boolean, right.
You cannot convert your data into some integer, float, or string,
or complex because list data structure is the combination of data.
[no audio]
Right. Fine. Now in case if I convert our data into Boolean
what will happen? Right.
So for that let me take, let me open my command line of Python.
[no audio]
Right. Simply let me copy
this as one list, no. This is empty list. Be clear, empty list.
So if I try to convert empty list,
[no audio]
right, into Boolean, see that, 'False'.
This is very, very important. Empty,
if anything is empty, if you convert into Boolean that is
'False'. Now instead of empty
let's say some random data, right, any data, now if I try to
convert you will get 'True'.
So these two points are very, very important,
okay. So 'bool(
[no audio]
empty_list)'
[no audio]
is equal to
'False',
'bool(non_empty_
[no audio]
list)'
[no audio]
equals to 'True'
It's very useful case in your real-time, guys.
[no audio]
Okay, fine.
Now guys,
[no audio]
you know the string operations with the index values, or based
on index values in your string
you are taking, you are displaying particular
character from your string.
Let's say I have a word called
"hello". Already
we know this, just I am recollecting that concept. "hello".
So once if you define your string in this way, what will
happen? Your Python will assign the index value for "h" is 0,
the "e" is for one, second "l", third "l",
and then fourth, right?
So five let us are there,
0 to 4 indexes are there in a positive way, or in a negative
way - 1, - 2, - 3, - 4, and then -5. So you can go with the
negative or positive.
Right. So these are the index values for your strings. The same way
whenever if you define a list,
for list of values also Python will assign some index values.
Let's say this is your list then 3 is at index 0, 2 is at
index 1, 4 is at index 3, sorry 2, this entire thing is one
data in your list.
This is at index 3, and this is at index 4.
[no audio]
Right. I will do one thing.
Even negative way also,
so this is at -1,
this is at -2, 4 is at -3, 2 is at -4, and
then 5 is at -5.
I will take, I will write a simple Python Script to give the
clarity on this list with index operations.
Let me open my Sublime Text. So I am taking this as my
[no audio]
'working_with_a_list'.
[no audio]
Fine. Observe that, first thing, how to print your
list. So I am going to 'print(my_list)',
and I want to see type of your data structure.
Anyway, we know that is a list data structure but through
Python you can find that.
See that, we are getting class as a list.
[no audio]
Right. Fine. Now, let me comment this.
So guys, we are saying that Python is assigning some index
values for your list of values.
So very first value is 3. 3 is at index position 0. Now if
I want to print very first value from your list, or if I
want to take first value from your list, I have to take index 0.
Right. See the output what you are getting,
3. Then I want to get, suppose some "python",
I want to display this output.
So "python" is at one, two, three, fourth value. Fourth value means
index is 3 because you know index starts from 0.
This is 0, 2 is 1, 4 is at 2, and "python" is at 3.
[no audio]
Right. Now I want to get
[no audio]
fourth value, means index is 3.
[no audio]
Yes, you are getting, and this is the way how you can get your
particular value from your list based on positive index values.
Of course, you can also use negative.
Let me take 'print(my_list(-1))'. You know -1
means by default last value.
It may be string, or it may be list, while going forward it
maybe tuple also.
[no audio]
See that. Or let's say I want to get,
[no audio]
from last second value
I want to get. That means index value is -2. If you take
-2 you will get
from last second value.
That's it, right. Now
let me comment all this.
[no audio]
So this is the way how you can access your
[no audio]
list of values
based on index. And one more thing.
See as of now if I run this maybe with -2 or I can take this
also, no problem,
[no audio]
both will give same result, right, because in positive way
your Python index is 3, in negative way index is -2,
right. You can take anything, no problem.
I am taking suppose positive index, and see the output what
you are getting, "python".
Now guys in case if I ask, write a syntax to get from your
list this data and in that I want to display only "y" letter.
If I write something, it has to display only "y".
So first of all you have to get this data. To get that data
already we have a syntax, and that is a string. In string "y" is
second letter. In the output, just observe this output. Now because
of this you are getting this output. In this output "y" is
at second position, means index is 1,
no. Now see that what I am doing, and see what you are
getting by doing this.
You are getting letter "y" because that is a string. Again strings
we can access with the help of index values.
Right. Guys you just try to practice in this way also.
Then, you know in your
list, right,
I mean strings, if I write something like this,
what is the meaning of this?
Entire, I want to get from starting to ending.
This is the way, or simply you can write simply
'my_list' also will work. This also, will give your entire list.
But if I want to print something like first index to last,
'1:'. Now you will get '2, 4, "python", 5.6', because we are mentioning
1 no. 1 means index 1. Index 1 is nothing but 2 data; 2,
4, "python", 5.6
you will get. See the output.
[no audio]
Right. I want to get from 0 to last, same as your,
[no audio]
so guys this syntax,
or let me write all these syntaxes because all
will give same result.
This will be helpful for your online exams. 'my_list' or 'print(
my_list[:])'.
see all the results are same.
So if you want to print a list you can follow any one of this.
That's fine. Now what my intention is,
I want to print something,
like you know, first index to only
[no audio]
three more values. 'print(
my_list', from first index 3 more values. Then 1 + 3, 4.
Then you will get
three more values. From 1, three values.
[no audio]
So guys, you can also consider like 4-1 means range is
3, you are getting three values. That's it.
[no audio]
Okay, fine.
Now this is all about, yeah, we have one more thing.
[no audio]
So guys if I want to change a list of values based on index,
let's say as of now index 0 value is 3 but I want to make
it as some other value then 'my_list[0], I am writing some
45. See the output.
Yeah. After that, I need to print it,
no. Let me print.
[no audio]
Let me comment this line. Because if you print then only you
can see the modification, right?
So after your operation just try to print your list and see
the output what you are getting.
Your index 0 value you are modifying into 45.
Right. Your index value
you're modifying into 45, right?
[no audio]
Fine.
[no audio]
Now, let me comment all these lines.
[no audio]
And one more thing guys because of this data,
I mean this syntax, you are able to modify your list value,
right. You're able to modify your list value. That means lists
are mutable. At any time
you can change your list of values, part of your list, right?
So this is very, very important. So lists are mutable.
[no audio]
And once again, you know strings are immutable. Once if we define
your string, you cannot change the part of your string, but
see that, you can change the part of your list. Because this
operation is allowed,
no. That means you're changing some value in your list, so
lists are mutable.
Fine. So these are based on index, guys, operations on your list,
right? Let me take some other operations.
[no audio]
'my_list', I am taking.
Let's say 3, 4, 5, something, value.
I want to explain the different way.
Fine. Now
first of all, what are the operations available
with your list,
how you come to know that, right? Nothing
is there. Just to go to your command line, Python command line.
Already we have a 'my_list', right. Some list
is there. Simply do 'dir' of your list.
[no audio]
See. Guys, these are for your documentation
purpose because the list is actually an object for your
some of the Python classes, okay.
That's why these are the documentation related things.
So you have to know only without underscore whatever you
have, that are you our operations.
[no audio]
Right. Now, see what are the operations there - append, clear, copy,
count, extend, index, insert, pop, remove, reverse, and sort.
First let me go with append, insert, extend.
Yeah, I will go with first index because that is also one
of the important one. See let me, index is nothing but getting
an index value of a particular value. 'index()', let's say 5
index. What is the index of 5? 1, because 5 is occurring at
[no audio]
first position. Sorry, second value means index is 1, right.
Okay. Now,
[no audio]
then that is one thing. You may get doubt, in case if you have
multiple fives in your list, right. Suppose
I have somewhere here 5.
[no audio]
Now I am running. You're getting index 1 means this index
value. Then what about this 5?
So in case if you are getting 1 no, just try to search
after index 1. That means from index 2 just try to search
and see if there are, if there is any one more 5.
Yes, it is there at 6th index, means seventh value.
[no audio]
Okay, fine.
So you don't worry about if you have multiple values how
to work with that, we'll have a separate section for that.
There we'll discuss it. For time being if values are unique
how to work with them, okay.
Fine. Now, actually the good thing is 'regex'. We have regular
expressions. There we can do lot of things.
Meanwhile, we just try to no minimum things from your list.
Okay, index done. Then let me take count also. See count is
nothing but, let me print
[no audio]
your list '.'
[no audio]
'count()', any number. Let's say I want to count number 10,
how many times it is there in my given list?
0 times. I don't have any 10 in my list, right?
That's why are getting output as 0. Now,
let's say suppose
I want to count a number which is having suppose
[no audio]
Three times 5 is there, no. Now I am going to count number
5, how many times it is there in my given list?
Three times. That's it.
Okay.
Fine. Now that is about count.
[no audio]
Any next related operation?
[no audio]
Yeah, 'clear'. Very simple one. Clear is nothing but, see guys before
running my 'clear' operation
I am printing 'my_list'.
[no audio]
Then 'my_list.clear'. Then 'print(my_list)'. See the output.
You are simply clearing your list.
So that is the usage of your 'clear'. Guys, if you observe sometimes
I am doing operation in print itself,
sometimes I am doing operation separately.
The reason is, here you are trying to get the, fetch the information.
This operation is like counting, no. You're counting, you are
not modifying.
So modifying operations don't do with print.
[no audio]
Okay. Directly, whatever the operation you are performing
that modification will modify in the original data itself.
Because lists are mutable, whatever the operation you are performing
that operation will modify the original data itself.
[no audio]
Right. So that's why I'm not using this in 'print' statement.
When you will use 'print'? If you want to see the existing information,
that type of operations you have to work with 'print', or you
can also store that result into some variable and that variable
you can print. Let's say I want to count how many times 5
is there, so directly I am printing the count. Instead of that
what I will do,
'cont_five', any variable. Then in this way. And I can print this variable.
So why should we take extra variable and why should we print
that? That's why I am directly printing. But don't print this data.
Right, this is operation.
You're modifying something.
So if any operation is related to modification, that directly
perform in this way. Don't use print for that.
Anyway, after some practice you will come to know that but
meanwhile just remember that.
So 'clear' is done. Then let me go with 'copy', right?
See guys, if you want to assign one variable value to other,
let's say 'x=4', and I want to assign 'x' value to
'y'. I can assign in this way, right. Same way, suppose 'my_new_list'
I am taking equals to 'my_list'.
[no audio]
Okay, or 'my_one_more_list'
I am taking equals to 'my_list.copy'.
Both are same. Nothing there.
You're assigning this values to this one. This
values to this one.
Both are same, but there is a small difference that is, whenever
if you directly assign in this way, you are not going to
create a new memory location instead of that, instead of that,
be clear, suppose
assume that your list
is there somewhere in RAM memory,
same address is pointing to this variable also. Actually,
this is for your original variable.
Same address is pointing to this variable.
But if you do 'copy', other memory location will be created.
That address memory location will be point out to this variable.
That is the use of 'copy'.
[no audio]
Right. Let me prove that.
'print(id())', 'id' means, address location, right, 'my_list', and then
let me take 'id(my_new_list)'. Then I am printing the
last one separately, 'print(id(my_one_list))'. Just see the
output. See for these two you're getting same 'id' locations,
but for last one different. So that is the use of 'copy' operation.
Right. Fine.
So this is about 'copy', or assigning one list to other list.
So there are two ways - one is assigning same memory location
address, referring to same memory location, or create a new
memory location and store the result,
then that result we are assigning to different variable.
That's fine. Now, let me go with,
[no audio]
I will go with now 'append'. 'append' is nothing but, see as of
now what is your data?
See that. 'print(my_list)'.
[3, 5, 2, 7, 3, 8, 9], right?
Now, I want to add some new data at the end of my list.
That is nothing but generally, that operation is called generally
appending. So appending is nothing but for existing information
at the end you are adding something that is the meaning of
appending. Same thing is applicable with your list
also. Let's say 'my_list.append'. So guys for this operation also
I'm not using 'print' statement because this is going to
modify your data. Whenever if you are modifying your list
that operation
don't do with print.
'my_list.append', I want to append some number 56.
So after appending just observe the result.
[no audio]
56 added at the end, but I want to add this 56 somewhere
between 3 and 5,
or between 2 and 7, anywhere. In case if I insert between
3 and 5, after inserting my number, whatever I am going to
insert, that inserted data index will be 1 because 3 is 0,
5 is 1 as of now. But if I insert between this you are
getting, right. So see that, 'my_list.
insert',
[no audio]
let's say index
1, value 45. Then after inserting see your list.
[no audio]
See, between 3 and 5 you inserted
your data based on index position.
'append' is going to add at the end,
but 'insert' is going to add based on index position where
you want to insert.
So 1, index 1, I should have a value called 45. Then see that,
3, 45, 5.
So 3 is 0, 45 is 1, right. So 'append', 'insert' are used to add
your data to your list. 'append' is going to add your data at
the end, 'insert' is going to add
data to your existing list based on your required index position.
Right. Fine.
Now you have one more thing called 'extend' operation. See that
somewhere you have 'extend'. 'extend',
[no audio]
right. So we have seen 'insert',
Let me go with 'extend'.
[no audio]
So suppose
I have one new list called
[no audio]
5 and 6. If I simply do 'my_list.append
(my_new_list)', and after this operation, just observe your output,
how you are getting your list.
'append' means at the end you are adding your 5, 6 data.
As the list of, see last value is also one list, you're
getting square brackets,
no, but instead of 'append', instead of 'append' if I take 'extend',
[no audio]
now see the result. You don't have the square brackets. Directly
it is normal data. You are adding your list as a normal
data into your existing list. That's it.
[no audio]
So that is all about your 'extend'.
[no audio]
Fine.
So we're done with 'append', 'clear', 'copy', 'count', 'extend', 'index' also
done, 'insert' done, now 'pop' and 'remove'. 'remove' is going to remove
your value directly. 'pop' is going to remove based on index
position. Let's say I am doing first thing your list,
so as of now what is your list?
Let me check it.
[no audio]
So this is your list, right?
So I am doing some operation called 'my_list.remove',
let's say a value called 10.
[no audio]
Right. After this I am trying to print. Actually you don't
have a number 10 in your list.
But if I do that, see that, what you are getting.
x is not in the list. That means whatever the value you are
removing that is not in the list, right?
So in your real time whenever if you are doing this operation
before that you will take some condition,
'if' condition, then we will remove it.
If it is there then only we will remove. If it is not there
we don't remove it.
In case whatever the value you are having that value I want to remove,
right. So what are the values we are having? 3, 5, 2, 7.
Let me take 8.
[no audio]
Now see the output. After remove operation you are removing number 8.
Right. Now, let me comment 'remove'.
[no audio]
Now what I am doing is, you have 'pop' operation.
First, let me do this, 'my_list.pop'.
I am not giving any index position.
First see the output what it is doing.
[no audio]
So before 'pop' this is the data, after 'pop' this is the data.
Right. So by default 'pop' will remove the last data.
If you give index position, let's say I am giving index 2, index
2, or let's say index 0, now see the output. If you provide
index position it will remove that data.
If you don't provide, by default it will remove the last
data, last index value, right. And one important point
is there with your 'pop' operation.
[no audio]
Let me show you that.
So as of now, what is your list? This is your list, right. 'print'.
Actually, your 'pop' is modifying your data, right?
That's why actually you should not use your direct operation,
'my_list.pop' into print. But there is a special property
with your 'pop', that is, your 'pop' will remove your data at the
same time it also gives what is the data
it is going to remove. That's why I can use it in my 'print' statement.
[no audio]
See the output. It is removing 9, that is it is
giving. At the same time
that data is removed from your list.
That's why, see the output.
So only 'pop' operation will give
the data what it is removing, at the same time
it will remove in your original data.
Right. So that is the use of 'pop'.
Now let me comment this. Now we have
[no audio]
'reverse' and 'sort'. Let me do with 'sort' first.
[no audio]
Otherwise, as of now what is your data, guys?
See that output once.
This is your data.
[no audio]
Okay. Now I am doing something called
[no audio]
'my_list.,
let me do 'reverse' first,
[no audio]
and after 'reverse' see what is your list is there?
[no audio]
See before 'reverse' operation, and after 'reverse' operation.
So it is like, you know mirror image of your data.
Right. Now, let me comment your 'reverse' operation, and I am
going to do 'my_list.sort' operation.
So 'sort' is nothing but, it is going to arrange your data
in ascending order. See that.
[no audio]
Now if you want to get data in
descending order, first perform 'sort', and then do your 'reverse' operation.
[no audio]
Now see the output, descending order. Or else
you can also use shortcut as 'my_list.sort', here
you can write 'reverse=True'.
So instead of performing 'reverse' separately you can also
do in this way. Now see the result.
Yes, you are getting in the reverse order, right.
So guys, these all are the operations on your list,
okay. So in next video, we'll discuss about tuple.
[no audio]
Right. Okay guys,
thank you for watching this video.
[no audio]