Python lists are mutable, heterogeneous collections of values stored in square brackets, supporting indexing, slicing, and various modification methods like append, insert, remove, pop, sort, and reverse. Tuples are immutable versions of lists, created with parentheses, which are faster and can be used as dictionary keys. Both support indexing starting from 0, negative indexing, and slicing, but lists allow modifications while tuples do not.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
May Python Bootcamp Day 6Added:
Hello everyone.
Hey, hey, hey.
Welcome back everyone. So today we are learning one of the most important topics in Python that is list and tuples.
Hello Minina this one day. So lists and tpples now why they are important because they are used everywhere. Okay.
So in data science, AI, web development, automation, game development, in APIs.
Okay. So if you understand list topples collections properly your coding level instantly upgrades.
Hello brother Narish.
So now guys first of all we'll start by what is list.
Okay, if if you want a single line definition to just uh tell about a list in Python, so we can just simply say that a list is a collection of multiple values stored in a single variable.
Okay, suppose what we do if you have to go somewhere for uh suppose for shopping grocery items. So what we do uh if we uh we might forget some items. So what we do we prepare a note. Earlier we used to write in a know proper notes okay with pen and paper but now these days we just on phone or we give a WhatsApp message for the lists okay so what we do we create a list in a single message in a single note okay in a single page. So what is that page that page is the container and in that container in that page we write all the items which we want. Okay. Earlier if you are not talking about aware is we waiting for you to start.
Okay. Am I not audible guys? Just let me know if I'm audible or not.
Okay. No, I I can I guess I'm audible. Okay.
So before list and toples, what was the thing? We we used to create one variable and assign one value to it. Okay. So before that we we cannot assign multiple values to a single variable. That was not possible. If you have two values, so we need to store two variables. We need to have two variables. Okay, if I have to show something. Okay, let me just give it like this or if I do like this. So then it is it will give you invalid syntax that it is not possible and with obvious reasons.
Okay, I'm I'm I I think uh there is some confusion. I am P only. Okay, I'm using a different set of microphones. Okay, because of that maybe my voice can change a little bit. Okay guys, don't worry. I'm that person only.
Okay, because of the microphones uh some voice changes, modulations can happen.
Okay. So, yeah, that's the thing.
Yes, brother, I'm from India. So, yeah, that's the thing. Okay, we cannot assign multiple values in a single variable and that we already knew. Okay. So for that purpose there is a provision of list.
Okay. Arrays tpples. Okay. So these are some kind of things some kind of data structures which allows us to store multiple values in a single variable.
Okay. And today we are going to focus on Python list and tpples. So we are going to perform each and every concept of list and tpple in this particular live session. Okay. So I want you guys to just please pay attention and just be focused. Okay. So we were at the definition. Now I guess I have you guys have understood why list. So now let me just write the definition. Okay. So it will be just very simple that we will call it as a collection because at the end it is a collection where we store multiple types of data types. Okay. Or multiple types of values of different different data types. Okay. So a list is a collection of multiple values okay which is stored in a single variable.
Now this is the definition. Now let's try to just create a list and let's see the syntax. Okay. So first of all just like how you create a normal variable it's just same as that you have to just create a normal variable. Okay. So just give a name assignment operator. Now here we have to store multiple values.
Okay. So to create a list and to store the multiple values in a list what you have to do there is a small syntax which we which we need to remember. Okay. I'll not say okay just remember it. Okay just that square braces. Okay. So you have to put all the values inside the square braces. Okay.
So let me just do that like this. Okay. 10 20 30 40. So we have four values, four integer values actually stored inside the square braces which means we have created a list now with four elements in it. Okay. And what are those four elements? Those four elements are integer values 10 20 30 and 40. Okay. Now, not only that, one more thing is there which is noticeable that is see um to separate between two values means how we are going to separate 10 and 20. Okay, that 10 is a different element and 20 is a different element. Okay. And not with the integer values with any kind of value you can do it. Okay. So, how you're going to separate it otherwise?
Okay. So, for that we have to use comma.
And now if I'll not use comma so it will be treated as one single value 10 20020 but because I have a comma in between so then it separates that 10 is a different element 20 is a different element 30 and 40 is a different element. Okay. So this is how you create a list in Python. It's so simple.
Okay. But one more thing is there only creating a list is not the thing. Okay.
Just you create it. No. There should be some meaning to it. Okay. So let's just understand list now. Okay.
That what are the features of a list.
Okay. So if you'll talk about the features of list. So I'm just going to simply write all the features and then one by one with the coming examples you guys will automatically see it what I mean. Okay, first of all about list is okay and okay one more thing people get confused with list and arrays okay they they think mean primarily in Python okay not in any other programming language but in Python there is a built-in list okay so because of that people many time get confused with numpy arrays arrays of python and then with list okay so guys let me just tell you they all are different python has its own arrays also Python has its own list also and Python has a library called as numpy which is specially used for uh arrays. Okay. So numpy also provides us arrays. So you can use that. Okay. So yeah, Python has its own array also. Okay. And people get confused in all these three things.
Okay. So there is no confusion. Numpy arrays is a different where you can apply the numerical computations and all that is mostly used in data science.
Okay.
U Python array that is also a built-in arrays given by Python. Okay. But there are few things in which it is different and Python list is totally different.
Just with the name list it's like that only in list what we do in list when we create a list it's means we write random values that values can be integer that values can be string that values can be boolean. That means any random values we just store it irrespective of its data type. But in arrays and numpy arrays we cannot do like that. Okay. It should be of same data type. All the values of should be of same data type. That is the main concern with the arrays that is not present in list. So list is heterogeneous in nature. It means it is a scientific word heterogeneous which is which means mixtures of elements. Okay.
Mixture mixture of elements. Mixture of elements means that different kinds of values of different data types can be put together in a list. Okay. And can be accessible also. Okay. So that is heterogeneous.
So list are heterogeneous in nature.
Okay. And then indexing is allowed.
Okay. It means uh the first value will be at zero index position, zero position and the last will be at minus one. So both positive and negative indexing is allowed. So indexing is there because of indexing duplicate values are also allowed. Okay. So you can have two elements which are same. Okay. Why it is allowed? Because um you get the indexing. So every element is not stored as a value. They are stored in reference to their position. So because of that duplicates values are allowed. Okay. So these were the few features few unique features of list which separates it from array. Okay. I hope you guys have got it. Okay. So, let me just write this thing so it will be more easy for you guys. Okay. So, first is that it is okay. Pardon with the spellings. I feel that I am correct but if I'm wrong so I am just opalizing it now only and I'm making an apology. and then heterogeneous. Then there is I guess and then there is indexing.
Oh uh then one more thing duplicates.
Now these are the few unique things and okay it's not that that these are the only things okay one few more things I remember is that u uh it takes more space in comparison to arrays and in comparison to tpples okay and uh and why it takes more space because uh it has more functions it has more methods list is uh totally flexible it means you can add elements you can delete elements you can modify elements okay so there are there are couple of methods are which are available in it which we can perform which we are going to see in this live session okay so no need to worry we are going to look out at each and everything just I'm letting you guys know that what are the things that is primarily to list and you guys must know okay so one more thing is there that it takes uh more space okay and because of why it takes more space because um because of the modifications which we make. Okay, so it is quite flexible in that manner and it takes more time also. Okay, it takes more time.
Now why it takes more time? Again it becomes very obvious it's take more space. So it is larger. Okay, so because of that the memory optimization is high because of that it takes more time. So everything is interconnected with each other. Okay, but yeah that is the thing.
we'll see a a separate uh separate difference between list and tpple there we are going to discuss these two points more deeply okay so no need to worry okay so these were all more two things which I just added now okay now let's just move further and what I'll do now I'll now give you a real life example okay so what I'll do okay let me create a list of students okay so let's say there is uh there is Mina.
Okay. Then there is Narish then let's say there is Rahul and let's consider me there is P also okay so there are four students Mina Narish Rahul and P okay so this is how and these all four students are willing to learn Python so they are the students of Python class. Okay. Now I just want to print it. So what I'll do just write the name of the list inside the print function and that's it. You will get your list and all the items all the elements present inside the list you will get it. Okay. So that is the thing.
Now you only see you can store numbers, you can store strings and not only that if I have to add let's say if I have to add a number also if I let's say if I have to add a boolean value also let's say I want to add a floating bond value also. Okay. So you can do that also in a single list. Okay. So this proves that it is heterogeneous in nature. It means it can store numbers, strings and other mixed data types also. Okay. In single list. Okay. Now let's go forward in the next cell and let's just try to now u go to the next step which is how to access list elements. Okay. So using indexing we are going to access the list elements and let's see how you're going to do that. So now those who are those who have joined the yesterday's session okay um 26th uh May session so they will understand because I have told you one thing that the indexing which we are using in string the same indexing the same slicing are going to be used everywhere in Python where indexing is supported okay so again I'll just start with the one thing that indexing in Python always starts from zero until and unless you change it to some other uh let's say you using the enumerate function you change it and start it to start with one. So these are the separate things but by default it starts from zero. Okay so indexing always starts from zero. This is one thing which you have to keep it in mind and with that said let's move forward and let's try to access elements. So let's try to access Mina host. Okay.
So how we can access the name Mina which is the first element in our list students. So how we can how we can access it? So for that it's very simple just like what we are doing in strings.
You have to just write the name of your list. You have to use the square braces.
Okay. And then indexing always starts from zero. Mina is the first element of our list. So what position will be it will be present at zero obviously because it is the first element. So let's write zero and just print it.
So now you'll see we have access mina.
Okay, means we have extracted Mina from the list. Okay, the first element. Now like that we can access the second element, third element, fourth element.
So let's just do that. So now I'll do what? I'm not going to write it. I'll just Okay. And I'll just change the index position. Let's say I want to now access nourish the second element or let's say the third element. Okay. So now Narice is present at the position one. Rahul is present at the position two. So let's just we have changed it and let's see the output. Mina Rahuli so efficient it is okay and so easy it is you have to just remember the position and you don't need to remember the position you know that from zero it starts then you can count it okay so it becomes very easy now one more thing um let's say uh negative indexing negative indexing okay again if the elements are from the end you want to extract or access the elements which are present at the end of your list. Okay. So what you can do now?
Okay. So then you get the support of negative indexing. Okay. It means that you can uh the last element is always present at minus one index value. Okay.
It means that uh in our case in the student list okay I don't know why I'm just to guys show you that it's multiple. Okay. So I'll just remove it.
Okay. I'll put this.
Okay. Now it's students list perfectly.
So now I want to access PU. Okay. Now here are only four elements. So it is easy for me to count it from the left side also. Okay. Means I can count it from here also. It's easy. But let's suppose if you have 10 elements or let's say you have 20 elements in your list.
Or let's say you have 25 elements in your list. So then then it will be difficult for you to count okay the elements which are at the end. So by using positive indexing normal indexing it will be quite hectic to you to just count. So then what you can do it's very simple use negative indexing. How you can use negative indexing. Okay. If you want to access P just write minus one.
So the last element is present at minus one position. So if you do that you will be able to access P now. Okay. Same thing you can do it for the rest of the elements. So minus1 minus1 is for P for Rahul minus2 minus 3 and for Mina minus 4. Let's say I want to access Rahul now.
So what I have to do I have to now write minus2. So now Rahul we got Rahul then um Nares minus 3 Mina minus4. So you can go either the ways whatever ways you want you can go it and you can check it.
So this is both the kinds of indexing it's upon you. Whatever the way you find it easy you can do it. Okay. Uh both the ways will work. Okay. Fine.
And believe me when I also use it when I also create list and all. So whatever the thing I feel convenient, I use it.
Okay. So it's it's totally upon you guys. It's totally upon the developer how you you want to write your code.
Okay. How you want to use your elements.
So and most specifically you don't write the static means a static elements static positions that that is set to be dynamic. If you are creating an application so you're not going to target one specific element always.
Okay. So yeah I I I I guess you guys are getting a point. It's just for the learning purpose. Okay. When you are going to build a real application, real web applications or applications. So this is not going to be static. It's going to be dynamic in nature. Okay. So yeah. Okay. Now let's come to the next thing that is uh that will be slicing.
Okay. Again just like in the string how we have used it. It's going to be very simple. Same actually same. You don't need to do any extra thing what we have learned yesterday. same thing is going to be applied today also. Okay. So, but okay, I'll again um repeat it because that's the thing. Maybe there will be few of you who are joining today only. Okay. So, for you guys, I'll just repeat it. Okay. And for those of you who have attended the yesterday's session, okay, you don't guys no need to hear it out or you can just revise it.
But it's the same thing. It's the same thing. I'm going to repeat the exact same thing. Okay. The words can be different but the meaning is same. Okay guys uh first of all what is slicing?
Okay. So for explain you slicing I'll explain indexing. In indexing what we were doing we were accessing one value at a time. Okay. So we were trying to access one value or not say one value we were able to access only one value.
Okay. What if what if if I want to access both Mina and Nares or let's say Mina Narish and Rahul not P. So then can we do that? Yes, that provision is also present but not with indexing. So that provision we can do it with slicing.
Okay, so we'll do that. First of all, let's practice here in this list. Okay, so we have a list 1 2 3 4 5 6. So there are six elements in this list. Indexing starts from zero. I want to access the first three elements. Okay. So, is it possible for me to access the first three elements? Okay. Not a single element at a time. All the three element at once only. Yes, we can access it. So, using what? Using slicing. So, let me just first show you guys how to access it and then I'll explain how I have done it. Okay. So, 0 1 2 I'll write three. So 1 2 3 I'm I am able to access the first three elements of my list that is 1 2 3 as you guys clearly see it. Okay.
Uh uh sry YouTube channel to extract a specific portion of a list is known as the string. Yeah is known as slicing.
Yes correct that is also fine. Okay.
So yeah this is how you're going to write. Okay. uh this is the particular portion which we are going to write here to access the first three element now let me just explain it to you okay or I'll not leave it like this I'll just make sure that okay everything is uh from the basic level okay because most of you will get confused in this also okay now 0 col 3 I've written here and I'm able to access the first three elements okay now that's very nice but what are this thing why we have use colon and there there can be multiple question in your mind okay guys I can understand okay so see first of all you have to understand one thing whenever you are trying to perform slicing okay so let's say numbers is my list name so first of all understand this start end and step so these are the three values and these are the three values purpose so the first position the first value which you write is for the start.
Okay. So you write the first position the first value at the first position and that is taken for the start. So whenever what value you write suppose I have written zero it means I want to start slicing from which position from zero means from the first position.
Okay. Then I have written the end position that is three. Okay. Three means the end position. It means that till what value you want to extract.
Okay, till what value you want to access. Okay, your element. So that was three here. So count the positions. So if you'll count the position 0 1 and two. So three is at the two position.
Okay. So why I have written three? That is the big question. I've explained it yesterday also and quickly I'll just explain it now. See for that you have to understand one thing which comes with the start and the end part. In the start part it is inclusive in nature means whatever value you are going to write that will be included. It means if I'm writing zero it means zero position is included. It means that it will start from the very first element because zero is included because it is at the index.
It it is at the start part. Okay. In the end part it is the vice versa. It is the opposite. The end part is exclusive in nature. Means whatever position we are going to write whatever the position value you are going to write that will be excluded. So if if the three is at the two position okay two index position and if I'm going to write two so clearly you will see that it is excluding two means it is counting till only one. It is not counting two at all. So then what we can do? So then there is one provision. So to make count the second position you need to give one greater value. It means if you want to count till the second position which is the value three in our list numbers. So you need to write one greater value that is three. So that's why initially I have written three there. Okay. By writing three what will happen? Now it is excluding three means which number is present at the third position 0 1 2 and 3. Four is at the third position but it is excluding three the third position means it is excluding the four number.
Okay. So that's why it is including now two. So it is included till two only. So that is the simple thing. No extra hidden logics. No extra any science rocket science. Okay, it's just that the end part is exclusive in nature. So whatever the index position you will write, it will be excluded. It will be not included. Okay, so because of that you have you need to write one greater value. So if you want to extract till three, so the position is two. So you need to write three. Okay, I hope you guys have got my point. Okay, so that's why that's the thing. And one more thing if you are not writing any number if you're writing suppose n okay suppose you're taking an input and you are writing n here so then what you can do so then it's very simple write n + one okay I'll show you so what is your position your position was initially two three is at two position do + one and you'll get the same thing because at the end it's making it three only 2 + 1 three and three will be excluded so it will give you till two only same thing so you can use + one you can use one greater value it's upon you it's totally upon you what you want to do it then uh the last part is the step part okay for the step part what I'll do I will just uh take one more print okay I'll take one different example because I don't want to make it everything in one example only okay now the step is what step means that skipping Okay, how many elements you want? Means which elements you want to skip. So you can skip based on the steps. Okay, means uh let's say okay I'll do what I'll make it like this. Now first of all I want you guys to just analyze this second the the marked list you get the elements 1 3 5 total how many elements were there? 1 2 3 4 5 six total six elements are there.
Out of that when I have written colon 2 okay it means I'm leaving blank the start and the end part. It means take the full list. Okay I'm leaving the start part as it is. It means it will start from the very beginning. I'm leaving the end part as it is. It means it will go till the end. Okay. So it will start from the very beginning. It will go till the end. It means it's taking it is considering the whole list.
Okay. So start and end is blank. It means it it's considering the whole list and in the step I have mentioned two.
What do you mean by two? Okay, it means that take the first and skip the second.
Take the first and skip the second.
Okay, that's why that is what mean by this this step two. Okay, step two means that take the first then first and skip the second. So two will be skipped. Then it will again take the first skip the second. Okay, then it will again take the first, it will skip the second. So that's why we are only getting 1 3 and five. Okay, because it's skipping the second number. It's skipping the second number. So 2 4 6 they have been skipped.
That's why we are not able to get it.
Okay. P Kushwaha says if in case okay your name is also PE that's very nice okay first time I got someone with my name only if in case we start the number with okay so let's say let's set the range uh it will go till five let's write nine okay so push you saw what happened here.
Okay, you got all the elements but it does not give you the index out of range error. Okay, because here you are performing slicing because of that. Okay, but if you'll do the same thing here, so then let's say here if you do the same thing then you will get this index error that list index is out of range. Okay, but but if you're doing it in slicing so then there is no problem.
Okay, it will extract all the means it will extract the whole list for you.
Okay, because I've given a very greater number means total values are from zero to five only. Okay, till five only and I've given nine. Okay, so it will extract the whole list for me. Okay, but if it it was a greater list which has elements greater than 10 12 or suppose if there are 15 elements so then it will extract till that value only till 8 position only because nine will be excluded. Okay, I hope you got my point.
So yeah, that is there. I I hope you got the answer. So this was slicing. Okay guys, I hope you have understood. Uh now when we are talking about list, so list has one unique feature and whether I have written that or not. Okay, I have not written it. That is modifications.
Okay, we can modify our list means we can change the values, we can add values, we can remove values. So these are a few perks of using list. That's why the space complicity is there. So not I not say complicity the space it takes more space. Okay. So now let's just try to perform modifications on our list. Okay. So I'll just take which list? I'll take this students list. Okay, I'll consider this students list and now I'll try to change values. Okay, so I want to change Rahul to Rohit.
Okay, so let's see whether we are able to do that or not. Okay, so for changing the value to change a particular value first of all we need to access that value. So how we are going to access it?
Okay, we know it how to do it. First of all, Rahul is present at what index position?
012. So, write two here. Okay, simple.
Then use the assignment operator and to what value you want to change it. So, you want to make it ro. Okay, by default or by mistake I've written Rahul. Now I want to change it to Rohit. So do that and then just print is students and that's it. Mina, Narish and now Rohit and P. Okay. So pretty obvious it is change.
So this is how you're going to change values. This is very simple. You have to just focus on that what value you want to change. What index position or what specific value you want to change. Suppose you want to change Rahul. So you need to know the position of Rahul. So you need to access first of all the position of Rahul and then assign it to the new value. Just that just that and then use your list.
Now your list will be updated with the updated value. Okay. Now this is how you're going to change the value. Apply modifications. Now there are a few methods which is used to add or which is used to add elements, remove elements.
Okay. So we'll now focus on that and we'll see that what are those methods and how to use those methods. Okay. And where to use it, how to use it, everything we'll see it. Okay.
Allowed. Allowed negative number is allowed in step value. And if you use it, it will be for reversing. It will be for reversing.
later on in this session only I'll I'll show that code also okay so no need to worry I guess Mina resp I have started the class are you going to start the class today or not I guess I don't know what you are I am live now and I'm audible also and I'm interacting with people please uh refresh it okay or check your connection maybe some error will be there. Okay. Something.
Okay guys, now let's just focus on some important uh now one more thing which I like to tell you that there are many methods. Okay. But I'll focus on the important ones which is very important or which is most widely used. Okay. So I'll first start with the append.
Okay. And one more thing uh yesterday someone were asking that what is the difference between a function and a method because sometime I call it as function sometime I call it methods.
Okay. So there was one person who have just emailed me. Okay. So for you if you are present today so I just want to tell you see there is a very slight difference. Both does the same thing. Both is used for reusability purposes. Both are actually same thing.
But uh the approach is different.
Function is kind of universal. It means function does not need any uh any object to call it. I hope you're getting a point. Okay. And methods do need an object. Methods are specific to an object or a class. Let's say when I create a class, so I create a specific method of that class. Okay? So only if you're using the class and the object of or you have created the object of that class then only you are able to use that method on that object otherwise you cannot use that method. Okay, I'll give you a very beautiful example of that and function is kind of universal. Okay, if you want to give take a function example so print is a function. It means print does not require any objects. Okay, print is a universal function just like type type is a universal function. len len is a universal function. Okay. But methods just like yesterday you saw the upper method, lower method, capitalize, title, replace, split. So these are what? These were methods which are specific to the object type string. So for that you need to create an object of the string class. Okay. So you need to have a string property. When you have the string property then only you can use the string methods. Same thing with the list methods. List methods can be only used to a list object. So for using the list methods you need to have a list with you. Okay. And on that list you can apply these methods. Okay. So that's why you always call it that you do not you do not create a variable in Python.
Actually you create an object and variable is the reference name which you give to that uh object. Okay. So that's why Python is all about object. It's not about variable. But but it's okay.
People know it with the variable. So it's fine. Uh you can call it as variable also. But that is an internal concept which we you everyone have to understand because this is a very basic conceptual thing which should be clear to everyone and I hope you guys are getting a point what I'm trying to say.
Okay. So now let's come to the topic. I just go off topic. So append append is a list method which can be only applied to a list only. Okay. So append what is the use of append? So it is used to add items at the end. So in append only you will see there is end. Okay. So it's it's quite understandable that it adds the element at the end. Okay. So for that I'll do what? I'll just take a simple I'll just take a simple list. Okay. And I'll just keep very less values in it so that I later on I can add more values to it. Okay. And let's say I have to use append. So what I'll do I'll just simply say uh numbers.append and let's say 1 2 3 is already present in the list. So what I'll do I'll just add four in that. Okay. And I want to be in a sequential format.
That's why I'm using append. So that after 1 2 3 then only four comes means four should be added at the end. So I'm using append here. And I'll do what?
I'll not do anything. I'll just print now my numbers. Okay. So I'll print my numbers now. Numbers list. And you will see the updated list that is having 1 2 3 and four means four is being added at the end of the list. So this is how append is used.
So then so then most of you will ask that only this is the only method. No, there are more methods which can add. Okay. So there is one method called as insert.
Now it's not compulsory as always you have to add add only. Okay. So you might um sorry. So you might add want to add in the middle or at the first place and the first position. So yes there is a provision for that you can do that also.
But how you'll do that? So for that you need to use the next method the next list method and the method name is only insert. Okay. And that this name is very common. You can see it in arrays. You create your own methods when you create your own array link list and all. Okay.
So insert. So insert role. Insert method role is very simple. It adds values at whatever position you want to add it.
Okay. So when you there is a talk of position it means you need to mention your position also first. Okay. So yeah.
So again let's take the example of the same list. I'm not going to create the list in the numbers only. Okay. Uh in the numbers I'll use insert.
And now I want to insert at the beginning. Okay. So I want to insert at the beginning. So first insert takes two parameters. Okay. So in the insert method takes two parameter guys. First parameter it takes is the position.
Second parameter is takes is the value which you want to add. Okay. In your list. Okay. So I want to add it at the first position. So I've written zero. I want to add the value zero because 1 2 3 4 is already there. So I want to make it 0 1 2 3 4. Let's do that.
And then there is numbers.
Let's print it. So now you'll see you have 0 1 2 3 4. Okay. And it's so nice.
Okay. So it's mean it is in sequential manner. Okay. And we have created it. So yeah. So this is insert for you guys.
Okay. Uh let's say uh if you want to insert one more value. Okay. So what we can do? Let's uh I'll copy it. Let's do it in the next cell otherwise it will be hop cop. I don't want that. I want it to be neat and clean. Uh okay. So now let's consider this is the updated list. So this list if you are going to use the same list. So this is the updated list which we need to consider it. So this is the updated list. Five elements are there. 0 1 2 3 4 5 I want to add um after two I want to add uh let's say 2.5.
So can we do that? Let's try it. Okay.
So 0 1 2 it means at two position I want to add point not at two add actually after two. Okay. So 0 1 2 at three position. Okay. So at third position I want to add 2.5.
Let's see. Okay. So now you will see now this is our updated list. Uh this is our updated list. 012 then 2.5 then three and then four. Okay. So we have just do it and there is a method for that. Okay.
And that method is very simple. It takes two parameter. First parameter is for the position and the second parameter is for the value and that value could be anything. It's not compulsory that it will be a number only because you have a number list. No, list is heterogeneous in nature. So what their methods are also heterogeneous. They can take any value. Okay? So they can add any value.
So the 2.5 it just added 2.5. Now what happened to the 3 four so they take a right shift because one item you have added. So that's why they just shifted by one. So three shifted towards the right by one. Four also does the same thing. So this is what happens if you add any element in the middle. So no any other extra element will be removed. So then what will happen then it will be taking a sift. So all the elements now take a sip. That is what that is the reason why list takes more space because it is random means not random but you can do modifications at any place. You can remove a number and you can place a a string character there. So then what will happen? The size will eventually increase. So because of that list has to take its own size. So that if there is a increase and decrease so it will automatically do that thing. Okay. So because it is dynamic in nature. Okay.
Because of that it takes more space because of that it takes uh more time also. Okay. So yeah that is the thing. So we saw insert. Now we saw two methods for append and insert to add items to add elements in your list. So then we must see one um few elements a few methods to just remove elements also. Okay. So now let's just shift it and let's just come to the next method that is to remove.
Okay. So the method name is only remove.
Okay. And what does this method does?
With the name it is clear it will be used to remove. But how? Okay. So it removes based on the values. Okay. So you have to write the value name and based on the value name it will remove it. Okay. So for this I'll do what? I'll create a separate uh list. So let's say let's create a separate list because if I'll do it with the same list. No you guys will get confused with the numbers.
Okay. So I'll take everyone favorites.
I'll take fruits. Okay. Fruits are favorite to each and every person I guess because it's they are my favorite.
and summer seasons are on. So mango is one thing which you'll find it everywhere. So fruits and dot remove and as I said remove method takes remove method takes the it takes directly the name of the values the name of the elements okay whatever the values we have stored it does not take the position so let's say if I have to remove banana so what I have to do I have to just write banana as it is so I will write banana and let's uh print fruits Okay, I have make a mistake in spelling.
So it will give me an error. So now you will see you will only have apple and mango because banana you have removed it. I don't like it so I just removed it. Okay then guys next method to remove elements is pop. Yes is pop. So for that again I'll do what? I'll again consider the same list, same set of list which is of fruits and I'll do what? Fruits dot pop.
Okay. And now pop takes uh index position. So let's say if I have to remove apple. So I have to write the index position zero. And now you will see apple is no more there. It means only banana and mango. The first element you have removed it. Okay. So pop it's now again upon you. you want to remove based on value. So use that if you want to remove the first item or the last item or any middle item based on index positions. So you can use pop. Now guys with pop there is one more important thing okay which I'll like to tell you guys for that I'll take a new cell. If is that if you'll not write any position name or any if you not give any position value inside pop. So by default pop removes the last element. That is the reason when you create a stack, when you create a stack, you use the pop method. Yeah, because it removes the last element. Okay?
So if you're not going to give any position any position value there, so it will do what? It is going to remove the last element. Okay? So I hope it is clear and this is after the 3.7 update of Python. So yeah, that is also there.
Now the next method is uh the method name is short and it is used for obviously the name is sorting. Okay. So it will sort all the numbers present. So let's say let's create a list with uh I'll take a random values 5 9 2 3 4.
Okay.
And now we need to short it. Okay. So to short it, use a short method numbers and that's it. It just shorted it in ascending. Okay.
No, no, no, no. Um if both elements are same in list, remove function will remove the first element. Okay. Whatever the first position element it will matches it. It will m start matching it.
See when you use remove what happens? It will match with the first element. No, it will match with the second element.
Yes. Okay. Let me just show you. Let me just show you.
Banana. Okay. Now you'll see apple, mango and banana. You don't get the middle banana. You get the last banana.
It means it will remove the first occurrence of banana. So first occurrence of banana was at the second means one position. So it has removed that. So you got the last banana. Okay.
So it will remove the first occurrence one. Okay. It a remove method will remove one item at a time. Okay. So if the item matches also it will match with the first item. If the first item is only removed, it is going to remove that item. It is not going to check for the rest of the items. It it is based on the matching. Okay. It matches. It matches with apple. Not matched. Remove. Now it's not checking mango and banana because it has to remove only one item at one go. Okay. So I hope you you got my point and you got your answer.
Okay. Uh now this was sorting. Now in sorting one more type of sorting is there that is reverse sorting. If you want to do it in descending descending order means now we do it in ascending order. If you want to do it in descending order so make reverse equals to true. Now you'll get in descending order. So now you start with 95 432.
Okay. So reverse is there. Now uh this was sorting you are doing sorting ascending and in descending order. Okay.
Now there is one method to just reverse your list. If you want to reverse your list. So there is a method whose name is only reverse. Okay. So you can use that method.
So how you're going to use that method?
It's very simple. What you're going to do is uh let's take the same numbers.
Okay.
same numbers and you have to use numbers dot reverse and then print numbers and that's it. It will reverse your list. Okay. So now it again reverse it to ascending order. So it was earlier in descending order. We use the reverse and it make it in uh ascending order again. Now it was already sorted that's why it make it in ascending order otherwise it has just reverse it. Okay it this was your list it just reverse it and now this is your list. Okay, because it was already sorted that's why you are getting in sorted manner.
What about the same element in a short method? Okay. Short method. Yeah.
Oh, okay. You want to say that if there is one more nine. Okay. Yeah. So, it will do accordingly.
It will do accordingly. Okay. It will arrange like that.
If you have one more nine, so all the nine will be put together. 999. Now if you want to ask me which nine is where.
So let me just tell you uh the the first nine which has it founded it, it is here. Okay? Then this nine is here and this nine is here. Okay.
Uh use Hindi. It's better because we all Indians. Yes, brother. I also want to start a separate uh separate section in Hindi. Okay. The separate I want to create a separate playlist of uh the live sessions and teach full detailed Python in Hindi. But uh the viewership of Hindi people is very less. Okay. Then today I can see that there are more of uh Hindi people. But actually it's the opposite. There are mostly people since I have seen my channel analytics and there are mostly people which are from abroad. Okay. So because of that uh I just keep English and this is universal also you guys also understand it and uh people from other countries they also understand it. Okay. So it becomes very universal kind of thing. Okay. And we target universal audience. Okay. But if you guys want me to create a Hindi particular playlist in Python I can do that also. But it's just that you guys have to just say you guys have to u give the viewership because if you guys will watch it then only it will motivate me and then only I'll do that okay so that is the thing so reverse uh then there is uh the next function okay which is very uh now this is not a method it is a function okay so let me just be clear to you guys len is a function which is used to give the length of the sequences and list is also a sequence and length function is applied to all the sequences. Okay. So it will be applied to a string list set dictionary and numpy array. So to all the sequences it will be applied. So length and you have to just write your list name that's it. And if you want the specific number you can just directly print it or you can store it in some variable. Okay. So it will just directly give you the direct number. So the total length of the numbers list is seven. So you got seven here. Okay.
Okay. I have updated it here. Okay. I have added three nines. No, because of that. So I need to update this also. I need to update this also.
Now it will give me five. Yeah. Okay.
Okay. L Abdul says I'm from Kenya. Okay.
I understand. Thanks. Okay. See brother.
Okay. Uh a big shout out to you Liven brother.
So that is the thing that's that is the only reason. Okay. So length function is there. Okay. And a few more functions are there which are the universal functions which you can apply and I'll just quickly show you guys how you have to apply it. Okay. So let's say if you have a list which is of marks okay let's suppose.
So I'll just give a random marks. Okay.
So this is a list of random marks. Okay.
So then there are these three universal functions. Okay. Not universal functions functions which can be applied to any structure any sequences. not any numerical structures. So it will give you the sum, max and minimum. So the function name is sum, max and minimum.
For that you don't need to import any math module. The these are the direct functions which are applied. Okay. So you can directly apply it on your list.
So there is sum and okay there is there is max and min. So I'll just change it max and then there is min. So 295 90 and 55. Okay. As you can see 90 is the highest, 55 is the minimum and sum is 295 of all these marks. Okay.
So these are also the functions which you can use it. Uh now what we will do is uh now we'll try to I'll try to show you loop okay how to use loop on your list and it's very easy you have to just use the membership operator and that's why I always tell people that in Python the for loop is especially optimized only for the sequences. Okay, because Python has its own five sequences. String is also a sequence and it has its own four uh data structures list, tpple, set and dictionary. So because of that the for loop is optimized in Python. Okay. And I'll show you how. So let's say marks are there. So what I'll do for mark in marks okay print mark one by one and you'll see you are getting one by one all the subject marks. So let's say if I have to add let's say if I have to add what I want to add plus five marks in each subject in my list marks. So what I can do plus equals to five. Okay. So now you'll see I have performed an operation and that operation is to add five marks in each subject and I have added it in each mark. Okay. So in 90 earlier it was 90 as you can see 90 now it is 95 85 75 and 55 now it's 60. So it's so good that you can easily extract one value at a time and whatever the operation you want to perform on that element. Okay, you can do it and the same process will be do done for all the elements and in just three lines. So that is how you use loop through list. Okay. And uh this was a direct approach means this is using the for loop then you can use a range function also and uh that becomes very hectic but uh that is a logical way.
Okay, I'll say that is the logical way because most of the places you have to use that also. Okay, I'll show you where you have to use uh so let's say you have the uh I'll just do what I'll just create a nums. I'll just have five values in it. Okay, so if I have to use range function to start using the loop in this. So what I have to do is for i in range okay length because when we are using range you need to give the length because range uses the length function okay and based on that it will execute it and then what I need if if you want to print all the numbers of your list nums so you have to extract the values one by one. So nums I this is how dynamically you are accessing all the elements. Okay, means it will start for this length. What is the length of this num? Length of this num is five. Okay, so it will take five and because it is starting from zero. So actually the length is four. But range uses the same technique. End part of the range function is exclusive in nature. So by writing five, it means it it will consider till only fourth value. So it it will go from this I will go from 0 to 4 and then it will access all the five values for us from 0 to 4. So at 0 1 is there at 1 2 is there at two three is there at four at three four is there and at four five is there so simple. Okay.
So this is how you can extract the elements one by one using the range functions but then you have to use the length function and then the accessing technique you have to use it. Apart from that you can use the direct approach.
Okay. for marks in marks for num in nums for i in nums. Okay. And then you have to just print I and it will do your work. Okay.
So this is how you do it. Uh now let me show you a real world example then it will be more easy for you guys. Okay. So what I'll do now uh I have my list marks with me. Okay. So on that list marks what I'll do? I want that the person who has got more than 85 marks. Okay. So for them print excellent and for rest of them who have less than 85 marks print good. Okay. So I have to create a conditional statement using loop for that. So I'll do what for mark in marks then I'll give a condition if marks is greater than equals to 85 then I want you guys to just print okay not this mark and then print or irrespective of that I use a string okay so I'll just now write mark and excellent And in the else I'll do what? In the else I'll print. Okay.
I'll just copy it out.
And here I'll just change it to good.
So now you can see above 85 one is 90.
He has got excellent 80 good 70 good 55 good. So all those people who have scored less than 85 they are good who have scored more than 85 they are excellent. Okay so this is a real world example how you have to use it marks was the list. Okay you iterated the list using the loop for loop then you applied a conditional statement. Now what happened for each mark means for one by one each mark this conditional statement is executed. It's just checking if the marks is greater than 85 it will do it work it will do excellent otherwise it will do good and that's the thing this is how you use it. Okay.
Now the last topic of the list is the last topic of the list is uh that is going to be the nested list means how you can create list of list nested list or or two-dimensional list because till now we were creating a 1D list a normal simple list okay how we can create a matrix a two-dimensional list okay so then for that uh let's say I have to create a a list of students marks okay student marks so how I can do that so for that I need two list okay so the first list I want is will be for wait let me first do like yeah so the first list which the parent list which will contain all the child list now in the child list what I'll do I'll first give uh the first element will be for the student name and the second will be for the student marks. Okay. Then I'll create one more list one more child list for the different student name. Let's say Ria 92 let's say ammon let's give the marks uh 70. Okay. So we have created a student marks list which is a two-dimensional list in which we have the student name and the corresponding marks. The student name and the corresponding marks student name and the corresponding mark and both are different elements. So first element is the name. Second element is the marks of that particular student. Okay. Now this is how you have created it. So if I'll execute this cell now there is no error means it's it's okay. It's nice. So if I have to print this list how this list is going to look like? Let me show you guys.
Okay, name is different. Name is student marks. So I need to give this name.
Okay, so now you will see it will be double uh square braces. So it means it is a two dimensional list as you can clearly see and you are getting all the child list 85, 392, ammon 70. Okay, that's fine. That's fine. Okay, this is how it looks like. Okay. Okay. Now guys, if we have to access now the elements of this list, so then how we can do that?
So for that is very simple. Okay, let's do that.
So I'll just copy it. I'll paste it over here. If I have to if I have to access the first element. Okay, so what I'll do, I'll write zero. So what it's going to give me? It's going to give me the first element, the first child list.
Okay. So when I'm going to write zero, okay, it is going to give me the first child list. Remember I'm what I'm telling child list. Okay. It means it will give me this whole list. So if I change it to this one, so it will give me the second list. Second child list.
If I make it two, it will give me the third child list. Okay.
Okay. That's that's nice. That's good.
But but if I want the marks of Priya then how I have to do that then how I will access it how I'm going to access this 92 which is the marks of Priya. So then guys first of all you need to access the children child list where Priya marks exist. So that is the first list. Okay.
Now you have to take one more square braces just beside that just corresponding to that you have to take one more square braces. Okay. And then this will make sure that you are now at this position. Okay. And by creating one more square braces this means that now you are inside this second list. Okay.
Again inside this second list second child list again the index indexing will starts from zero. So Priya is at what position? So indexing also starts from zero and this is a separate child list.
This is a separate list. It is a child list but it is a separate list. So again indexing will starts from zero. So pri is at zero position. What I have to access 92. So 92 has what position? Zero one position. So let's write one and now you will get 92.
Okay. So this is how it works. So now if I have to access the name Priya. So what I have to do? I have to just write zero.
By writing zero it means I have access Priya. Okay. So this is how you do it.
This is how nested list work. Okay.
With that said guys list is now over. We have seen almost everything in list.
Okay. So with that the basics applications of list we have understood.
So you guys can go and solve basic problems based on list to add elements to create interactive list. Okay.
So that you can do okay with this everything will be clear. Okay. Now let's come to the next topic. Okay.
Because today we are going to learn two things list and tpples. So now we will talk about tpples. And believe me tpples are more easier than list because tpples is very less in comparison to list because tpple does not have some extra methods. Okay. Tpple is very specific pointtooint. So let's talk about the definition of tpple. So tpple is before the definition I just want to tell you tpple is very similar to list okay but there are few differences just like list was means in list we can do modification it means list was mutable in nature whereas tpples are immutable in nature so tpples are also the collection of elements but they are immutable in nature and because of the immutability you cannot change an item And you cannot add items and you can cannot remove items. So it is kind of fixed. And because of this feature of the tpple, tpples can be used as the key of a dictionary. This is one thing which I'll let you know when I'll come to the dictionary part. But for now, just mark this thing. Okay. So for the definition of tpple, I'll just say one thing. A tpple is uh again it is also a collection of elements but it is it is immutable. Okay means cannot change uh and irrespective of that it is also ordered. Ordered means indexing is present. Okay. So that duplicates are allowed. It has also heterogene it is also heterogeneous in nature. And so let's talk about the features now. Okay.
So if we talk about the features so heterogeneous in nature uh ordered there it means indexing is present.
Apart from that ordered indexing is there. Uh duplicates are allowed.
Duplicates are allowed. Apart from duplicates, uh few more things are there. It is faster in comparison to list but has less methods of it. Yeah, it has very few methods. It has not specific methods of tpple. It has the methods of please share WhatsApp number as I want assignments.
You can go and check the description link. I have given you the group WhatsApp group. We have created a WhatsApp group for May boot camp. Okay.
For May boot camp, May Python boot camp especially a group is created on WhatsApp on our WhatsApp channel. You can go and click on the description link. Okay. Let me just check it whether I have given that link or not.
Okay. Okay, I guess I'm not given it.
Okay, I I'll provide you the link. Okay.
After when the session will end, I'll provide you the okay. It is faster in comparison to list but less uh but there are less methods.
And one more thing uh tpple unpacking.
Now this is one thing tpple and packing is there and uh you can use tpple as dictionary keys. Okay. So this is also one thing.
These are the few features of tpple.
There could be many more but these are the most common ones. Okay. Most common features.
Okay. Apart from that let's now see how to create a tpple. So to create a tpple it's very simple. Everything is same.
Everything is same just only a little change. Instead of using square braces, you are going to use the normal parenthesis means the round braces. So you will put everything in the round braces and let's just print numbers.
So you'll get everything in round braces. If you want to check the type, you can check the type also.
If you want to check the type, just check the type. And yeah, class tle. So it is of class tpple. So this is how it works. This is how it looks like. So yeah that's it. Okay. This is how tpple is created using the round only.
Remember using the round braces. As simple as that. No need to go or do some extra things. Okay.
Okay. Now guys, uh let's see how we can access topple values. Okay, because indexing is allowed. So accessing also can be performed. So let's just see.
Okay, so what you need to do is first of all I'll use the same list here. So I'll print the same list over here. So it will be easy for you guys. And then now if you want to access the first element so what you have to do you have to just write numbers square braces and the first position indexing always starts from zero in Python. So the first position one is present at zero. So you'll write zero and you'll get one. So as you will get two three and negative indexing is also allowed. So if you'll write minus one you'll get the last element. So everything is same as list.
Indexing is totally same as list. And I've already told you if you have learned indexing and slicing in string so it will going to be applied the same thing in list dictionaries and everywhere okay carries. So yeah one more thing that is uh modification is not allowed it is immutable in nature. I'm going to prove that this thing immutable immutability Immutability means you cannot change it. Let's say in list what we are able to do just access the first element and change it to let's say 20 2 to 20. So it is not possible in tpple.
Tpple will give you a type error that tpple object does not support item assignment means you cannot assign an item or an element in tpples. So that's why tpple are immutable in nature means on a tpple is created it is created. It is fixed. Okay.
So that is there. So immutability is also there. Okay.
So this is one of the important differences which is one of the important difference between list and tpple. Now guys you will ask me and you can ask me a simple question sir if you have already lists why to use tpples.
Okay. So your answer is tpple are used for tpple are used for c guys. They are used for uh where you have fixed data. Okay, where you have fixed data. Fixed data means suppose if you have to store coordinates. Now coordinates are one thing which is fixed. Okay, coordinates never change. So for coordinates you have to when you have to store coordinates and all you can use tpple because by default also it cannot be changed. Then for database records okay you see in a data volume you in a database you create a tpple and then you store it if you'll talk about a normal u normal relational database okay so you store everything in a tpple format okay row and column so for database records it is very nice because if something is in database you don't want to change it so that's why tpples are best for that uh that purpose and they are faster than list so if you have to perform any operation where you need uh there is a foundation of time so you can go for uh tpples but if you want a flexible data structure if you feel that okay this is not uh you need to do some modifications later on on your data structures so then go for list there are specific use cases okay so this is one of the use cases of tpples this is where tpples are post.
Okay, one more thing is there that if you want to create a tpple of only one element. Okay, let's say of only one element. Okay, only five and let's print t.
So now if you will check the type of tino actually it will be not tpple it will be of class int means you put five in the round braces means five is inside the round braces. Okay so ideally it should be a tpple but that is not the case here. So the case is that when you are checking the type of t so it is actually of class integer not class tpple. So then why is it so? So to make it a tpple you need to give a comma.
Give a comma and leave as it is. Means don't give any value. Keep a single element only. But you have to at least give a comma. Comma will make sure that it is a tpple. Otherwise it will not give anything. No. So it will be considered as an integer only because of the board mass thing. Okay. Because the brackets are solved first. So when you have mathematical computations going on.
So to so to separate that okay execute this first for that kind of purposes this is considered as a normal integer.
So to make sure it is a tuple of a single element only you have to give a comma. Now this is very important thing people get confused on. So I just want you guys to make it clear. Okay. So yeah this is the one thing which I just wanted to let you guys know.
Okay. And yeah that's the thing. That's the thing.
Okay guys, so this was all about the tpples. In tpples there is no uh more things. Okay, there are few methods or not few methods there are only one method okay which is a length function which will be used. So irrespective of that there are no extra methods in the tpple. Okay. So this is enough and many of you yesterday in this yesterday session you guys were asking for uh assignment questions. So guys now you have gone through everything. So I'll write all the questions here and you guys can stop and pause it and just write the questions. Okay. So your question is to to create a list of five cities and print them using a loop. This is your first assignment.
Then your second assignment question is take five numbers and short them. And I don't I want you guys to do it with yourself. Okay? Don't use any kind of AIS or any kind of notebooks and all because see this is for you. This is and these are very easy also and if you are feeling hard go through the classes go through the class because go through the previous sessions also you can find out all the videos in in the playlist in the live session. So everything is already there. There is a separate playlist for May Python boot camp. So you just need to go and explore the playlist and everything you will find it. Okay. Third question is to find the sum of all the even numbers from the list. Okay. So create a list from 1 to 10 and calculate all the sum of all the even numbers. Okay. And that's going to be very easy. That's going to be very easy.
And one more important thing which I'll just now give you is uh to create your own students marks analyzer. Okay. It is kind of a mini project. So you have to create your own students marks analyzer. Okay. Now this is going to be your main task.
Okay. So question number 1 2 3 is for practice. Then last question is for your main task. What you have to do in this?
Let me just elaborate it. Uh you guys just need to uh student marks analyzer.
Uh you have to uh first of all take take the student marks marks of student marks. Okay. And then what you have to do then then first you need to calculate the total then you need to calculate the average.
Then you need to find find the highest marks. Then you need to find the lowest marks and then you have to count the past students. Okay. So this is what you have to do. So this is your main task student marks analyzer. Okay. I feel everyone will be able to do this because it's very easy in nature. So you guys will do it. Okay.
So let's keep this much only for this session. Okay. This was all about the day six. So if you guys like today's session and today's session if it helped you to understand Python list and tuple clearly. So then like the stream, subscribe to the channel and practice the student max analyzer project yourself. Tomorrow we'll move deeper into Python with even more practical coding and mini projects. So please stay tuned with our channel and be consistent. Keep learning Python. Keep exploring our channel and that's the only thing you guys can do it and I can do it. So thank you so much everyone.
Have a nice day. Take care.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











