Sunday, June 26, 2022

# Python | List Operations | Term 1 Revision | Class 12 | Computer Science

Hello students i have once again come with another uh video lecture on the topic of list operation uh so here also we are going to attempt 30 questions on uh list operations uh so let's see this is the first one which one of the following would give an error now.

We have four options here this is list this one and the second one is list multiplied empty list multiplied by three and then we are having the third one which is the list one is a empty list plus three and none of them now.

If you look at the options the a seems to be correct now we come to b b also seems to be okay now between b and c we have to decide which one is there now if there is an empty list and we try to uh multiply this list with the three so replication operator will it work for empty list so let's see we are having.

This list and here we are in this list we are taking this one and multiplied by 3 and we run it so we are not getting any errors so this one is correct but the one which we have to see is this one now if we run this one we are getting a error.

And you can see the error why because in list operation we can add a list with a list but not with the integer value so here this would give an error so this is the correct answer so let's see and check this one yeah this is the correct one now a second one is which of the.

Following is true regarding list in python the first one lists are immutable so immediately if you are reading it carefully it is saying immutable means you cannot change it which is not true so we are looking for a true statement so this is a false statement.

Size of list must be specified before its initialization no we do not have to do anything like we have to tell the size because uh it's a dynamic list dynamic so we do not have to do anything of that sort elements of list are stored in contiguous memory location yes it is true because that is the.

Reason why we can use indexing to extract the elements and the d1 is size list one command is used to find the size of list yeah list is size is like one of the functions which are there in size of in c c plus plus it is there but not here it is len which tells the number of element so c is the correct answer.

Yes c is the correct answer now here we come to this one uh what will be the output of the below python code we have a list with the value 8 0 9 5 and this is list one coulomb minus one now see this double colon minus one basically is a very simple one if you have studied in class 11 it's uh.

Simply reverse the entire list so let me show you if i run this one so you are going to see the values are like 5 9 0 8 so here 5 9 0 8 is the first one so this is the and this should be the correct answer yes this one is the correct answer now the fourth one.

Now now which one uh of the following will give you the output as 23 to 975 if the list is 26 23 3 2 0 9 8 75 now if you look at this one what we can understand the values are after 23 the next value is 2.

After 2 then 9 and after 9 is 75 so one of the things which we can understand is that the values are one of in between we are skipping one value so we can see it here this 2 which is telling to skip to one not going to directly to the next element but skipping one element it is.

Because of 2 23 means the first element which is 23 and now we have to reach up to 25 and remember in case of list if you want the values to extract the end value should be one more uh than the given the index till where we want the values so here 23 is there this is at the index 1 and let's see 20 75 is.

Where so it is 0 1 2 3 4 5 6 7 so if we want to include 75 we have to give the index 8 now 8 is here but this one is starting from 0 so if 1 to 8 if we do so we will be getting this value here as a result so c is the correct answer okay now this is the one which is saying uh.

The average marks uh the marks of a student of six subjects are stored in a list and list is this one and how can the average marks be calculated and we know the average what is the average average is the sum uh divided by the total number of elements so there is no such function as average.

Here we are getting sum so here also we are getting sums of b and c number of elements you know we cannot get it from the size but it is from the length so length function is going to give us the number of elements so the correct one will be sum divided by length so b is the correct answer okay uh next one uh what will be the.

Output of the following python code now we have a list with a certain number of certain words are there few words are there and we have to find the minimum of the word now it might feel like that the minimum will work only with uh like it takes a list but it doesn't mean that it can only.

Work with the numbers so let's give a try first let's see uh we write a min of this in the list okay so we are taking the main and list one so here if we run it we are getting a zero okay not a issue but let's see we take some um.

Um words here right red green and let's see the another one is the blue okay so we are taking this one and let's see we run it here now if we run this one you can see the word view is like getting but now i am giving you one more uh thing which you need to note is that if.

I give a red r and then i am in upper case and i if i run this time this r is coming uh this is happening because uh python creates uppercase and lowercase letters separate like uh differently in uh it's a case sensitive so with the lowercase letters are having a higher ski code than the uppercase letter so always the.

Uppercase letters will as i have a lower uh ascii code so they will come like in case we are finding the minimum value so we have to consider them first now in this case you can see uh this upper case are here and here also first so like in a dictionary we have to see which one will come so obviously this two will are the first.

One to come but out of this two if you look at the dictionary the c should be the one which should come first so the minimum value of this is c uh and uh to verify this thing i'm just taking you here and putting this one and just copying the code here and i'm running it here and you can see the c is the correct answer so c is the one.

Okay so this one is done now the next one is uh which of the following will give you give output as 8 9 10 2 3 uh so first we need to see uh here that this list one is there 6 23 is 3 2 or 10 9 8 75 so first is eight then nine then ten.

Then two and three uh the thing to be noted here is that this values are in uh reverse order so if you want the values to be in reverse order we have to give minus 1 uh so that it is going in this direction because if it is -1 if it is minus two so you need a minus.

One now we have to go up to this one so this is minus one minus two minus three minus four minus five uh up to three minus five uh uh but we need the other value also this is the one which is that 23 also we need to do so let me see minus 1 minus 2 minus 3 minus 4 minus 5 minus 6 minus 7 till -7.

And we start from if we look from the minus seven we have to go up to uh this is minus two okay uh minus two so minus two to minus seven uh it cannot be minus seven because minus seven minus one will be minus eight so it cannot be minus seven it will be minus two because we are reading in a reverse order so minus two minus three minus four minus.

Five minus six up to minus seven so uh in this case then the answer will be b okay so let's check this is the answer yes this is the correct answer now we are coming to this one what will be the result after execution of the ever python code now we have a list one with this set of values are there list.

One dot pop three three means the index three so index three is zero one two three so seven is the value which will be removed so if you remove the seven from here so three two five three six this is a seven is in rest all seven is present so we can safely say a is the correct answer.

Okay when we come to the ninth one now ninth one what would be the output of the following python code now here you see we have a list and we are removing the value two now the problem is here this remove two means will it remove both the twos or only one uh as we know uh so instead of going to.

This thing before i come to the answer i tell you what exactly it is doing if you write this thing and then you print the list one so if you're printing the list one what result you are going to see so in that case you're going to see the result it has removed only.

Uh the first occurrence of two so if we remove the first occurrence of two then what is left is 1 3 which is 4 4 plus 5 is 9 and 9 and 10 okay 9 and 10 is 19 and this 2 which is 21 so here the sum is there so it is 21. okay.

Next is uh what will be the output of the below python code we have a tom mary simon and insert 5 8. now the thing is that before we come to this uh two three things which we need to understand is that in a list function okay so we take a list.

Uh not a function in the list uh if we are taking some value like c4 and if i am doing list dot index the index which is not here is let's say 10 10 index is not there and i am putting a value 5 here and if i print the list what will be dow so let me run it and what you see uh.

It is uh the list here the index is there oh no it's i'm going uh not the index is the thing it is the insert okay so i'm making the change now with the insert if i see this one uh what you will see is one two three four and five so irrespective any bigger index you will get this result and if you are doing list dot insert and you are giving.

Let's see minus 10 which is also not here and we give some values like uh minus 5 and if we run it so you will see the result is here so what is the understanding the understanding is there for any bigger index which is not present it will be at the end and for any smaller index which is not.

There it will be in the front so in this case the here it is at the 5 8 so the answer should be b okay so this is the b so this is one now 11 is uh what is the output of the.

When we execute uh the list hello okay uh it seems uh this two doesn't looks like a correct answer but between a and b we have to decide many may think that this is like a hello will come like this in a list but uh python will take this as a string literature and it will treat each of the individual.

Elements uh as a when you are passing to this function so if you see it here uh if you're writing this thing okay i'm writing this thing and let me print this thing so if i'm printing this part and so this is going to give you a list now.

What is there in the list which you should see so when we run this thing what we get we get a list uh all the in this string whichever words letters are there each of the letter is like separately it has come okay so i think uh this is uh so we can safely say this is the and it is in the form of a list so.

Uh a is the correct answer okay now we come to this one find the output we have a list is there amit sumit nana and here l minus 1 and minus 1. now we have to understand one thing here this first minus 1 means uh the last element okay and this second minus 1 is the.

It is again considering it's again a kind of a string now in this string which is the minus one means the last character last character is a so the answer will be here c so let's check this one yes answer is c now we are coming to this one uh write the output of the following code we have one two three.

Four five for i in l print i and i equal to i plus one now uh before we uh again come to the answer part answering part let's see we see what exactly is happening see it is going to read every value from here so i will be having a one value and this value which it is there it is a like a kind of a constant value here it.

Will be getting all this one two three four five and then we are changing the value but ultimately when it is coming here it is not going to affect the well next value which is it is reading from this list so it will be one two three four five that is to be printed so in this case this is to be printed and others are not.

To be uh are not assumed to be are not the correct answers okay fourteen one so write the output of the following we have a t equal to one two three four five five point five and list uh l equal to list t and now three multiplied by two point.

Five so three means the third element so zero one two three four four is multiplied by two point five the four two point five is twenty five four means the hundred so decimal point so it is ten now the question is whether it's the b or c and you see a decimal point is there so.

That's why the answer will be also in a float okay so that is the thing which we always have to remember when you multiply a float and integer we will get the result in the form of a flow now the next one is uh write the output of the following uh t equal to 1 2 3 4 t 1 is equal to 3.

4 5 6 and t two is equal to t plus t one uh it should have been a print statement but let's see we are thinking in terms of the t2 now t2 is the one which for which we do not know anything so what is t2 here and the plus operator you know it is a concatenation operator it joins the values of one two three four with the three four five six so the answer.

Will be in this case will be v okay so let's check yes so this is the correct answer now write the output of the following we have t equal to one two three four five and t one equal to three four five six so t one dot append t one now a single value will be appended which is t one so one two three four three four five six.

So it seems to be the answer okay right and uh this will not be and none and none of the above doesn't look so but is it so no we have to look what this change has happened in t we are printing t2 and this append function has no return value so when a function does not return.

Anything it implicitly return none so it should be none here okay so it's a bit of tricky but let's see uh here we checked also this thing uh i will is it correct so we let's run it so it is none so you have to check that this thing which is t dot append it is going to affect the t.

But as it is a void function so it is going to return the value now okay so we come to the next one a dell statement can delete the following a single element multiple elements all elements along the list and all of them okay so let's see uh what is the del is doing so let's see we're doing del t 0.

So it is printing we if we are printing we are deleting the only single element okay so let's run it so we can see one has gone now we can do here one two two that means zero and 1 so 1 and 2 should can be deleted yes 1 and 2 and if we do this t delete the entire thing so is it possible.

Yes it is possible why because it's after this line it is not recognizing the t that means after this line it has removed the t all together from the memory and so print t is giving a name error so in this case uh we can say it is possible for single multiple all and all of them will be the correct answer.

Okay now we come to the next one write the output of the following t is one two three four t one equal to t t zero x a 80 t1 now before we come to this answer i would like you to see let's see this is t1 and uh this is t2 okay t2 and we see it here this t1 now if i print.

Uh print the t1 and i print t2 so if i print t1 and t2 what i am getting i am getting the same value but the confusion is there whether this t2 is a separate copy or same copy so what i am doing is that i am writing t1 first element.

I am replacing it by 10 and now again i am printing the t1 and t2 let me see uh if i run this one what i will get yes what i am getting is that it has changed modified the first element in t2 so that means they are both pointing to the same memory location so if i'm.

Making it 1a the other one will also be a so b will be the correct answer okay this is one now the next one is write the output of the following we have physics this is the one and then chemistry this is two then math then three then four then five and six remember in the len function we actually.

See the actual counts which is starting from one so there are one two three four five six six elements are there so c d will be the correct answer okay now we are coming to this one which is uh write the output of the following l equal to one two three four five six seven eight nine ten and l l three now you see llc then in the list which.

Is the third element so this is zero this is one this is two this is 3 so 4 is the third element here so we are getting here 4 now again it is as it tested so l is 4 so l 4 means 0 1 2 3 4 which is 5 so fifth one is the correct one okay so fifth one it should be the correct answer in this case.

Okay now we are coming to the 21st let me make it of this uh so in 21st we have list one two then list three and four now append append function will add three four in this list so we can say c and d now we come to list three which is five six and extend 3 so what it is doing is.

That this values of 5 and 6 will be now added to the list okay so append always add a single value and it will add all the values which are there in this list so in that case c should be the correct answer okay so uh if you want even to see this thing you can um just copy it and.

Let's see if we copy and check here and if we run it here and we can see it is the same answer okay so we select this one 22nd i find the output of uh there is a function with the list empty and then we have 4x in scope this is double colon minus 1 x not in list and x mod.

2 is not equal to 1 then we add it so a little bit we do the working of this one you can just see and the working and we will not execute exactly so here what we are getting is x will take all the values of arr in the reverse order so the values will be like.

Five six uh three two or four uh then again two then three and then one now here what it is saying if x is not in list okay so 5 is not in list and if it is uh.

Not divisible if the mod of the value is not 1 that means if we can say if a numbers mod is one that means it is a odd but if it is not one that means it is a even so if it is not in the list and the even numbers will be added so in this case six is the even who is the even four is.

The even and two is the even again uh but again there's a one issues here if it is already there it will not be added again okay because if it is not there so six uh two and four okay so this should be the answer if it is there so this is the answer case.

Yes so this one is correct all right now the next one is which line numbers will cause error now here my list my list plus 60 yes line number two is already we have discussed that you cannot add an integer to a list count uh remember if you recall count can return if a.

Value is not there so it returns zero but index on the other hand will raise error so we can see line number two and line number four uh two and four uh line number five is also a bit incorrect because here this variable is not there but there is no such option where we can see all the two and this.

One and i think it's just a mistake so considering it's a typing error so we are leaving the five we are taking the second and the fourth one so let's see see and check whether it is the correct answer yes it is the correct answer next is this one okay so here we have 24th one we have this values are there.

So one two three we have 20 and 30. so one two three means element one and the one and two is 22 and this is replaced by uh 20 and 30 okay i just show a little uh here.

So that you can understand here so 1 and 2 means if this entire thing is there in this case let's say this entire thing is there so here 20 and 30 is going to replace one and two so it will be replaced here okay oh it's not taken so let's see this we are taking it here so one and two is going to replace.

Uh this value okay next is 40 and 50 so 40 and 50 is uh like three and five so three and four so zero one two three and four so this will be there so we are going to put zero one two 3 and 4 means this two are there so we are getting 11 20 30 40 50 60 this should be the output uh you can run it here also and you can.

See we are right so in that case uh 11 20 30 40 60 and uh we have 66 also okay so 66 so 11 20 30 40 50 66 okay so c is the correct answer so let's see yes we are right uh next one is to find the output modify.

The list for i in range lane of list if a list i mod 2 equal to that means if it is a even value we will multiply by 3 and if it is an odd value we are going to multiply it by 2 so 5 is a odd value and we have to multiply odd value by 2 okay so 5 should become 10 so i think b and d.

4 should become a 12 so this time now you can see there's no possibility of d so this and rest we can see 3 is again at 6 2 is 6 and 1 is 2 so b will be the correct answer okay so the next one is we have this 1 26 of 30 the change of list list is equal to list colon minus 1. now this is.

A very important one which uh slightly bit confusion confusion is there so uh why i want to show you first that if we are doing here let's see we are not calling the function but if we are print here.

Uh err square write it colon minus one so what it is going to print is it's going to print uh this list without five okay so this is the one understanding of this thing so here we are getting something without the five so if we are taking this thing and we are.

Getting it here this entire thing as here five without five okay so list is modified and then we are doing it here for i in range list and then if it is a divisible by three uh we've written here one if we divide by three and we get a result 1 then we multiply it by 2.

But before we come to a solution part i would like you to focus on a very simple thing which i am telling you not writing this code uh if i am printing the id of a list here and the id of list here okay id of list here and if i'm running it here and you see that.

I think uh i think i've written list id listed oh this will be arr so let's see err and now we run it this one so what you will get it here you are getting the same id okay so at this point when you have passed it you're getting the same id but let's see we are doing the change which we have done in in this is.

Colon minus 1 and then we are again printing the i so if we run this one what we are getting this time that uh till this point id is same but the moment you are like uh changing uh by like doing the slicing operation we are extracting so this.

Operation is basically returning a new set of values which are stored in a new memory location which is like by this particular uh id objects this which is a different from the one which we have discussed so any so for your understanding i'm telling you for.

A list whether it's a replication operator or whether it's a concatenation operator whether it's a plus this is a multiplication or this indexing this will always create a new memory location which is like so if you pass it here so after this point you are getting a new memory location the original one is not.

Uh now like corrected with it so uh if you print it later on so you will get the same values so in this case uh i'm just showing you the output directly so that you can understand why the output is the same one so you can see it is 246 there is no.

Change because from this point onward whatever you are doing it is internal to this another memory location and ar remains same as it is so in that case we have 2 6 4 12 9 5 is the correct answer okay so we come to the yes this is the correct now the 27th one now 27th one is a.

Little bit of confusion is there so let me first clear the confusion from your mind what is the confusion why this functions each mean uh first this sorting will arrange the element in ascending order ascending order means 11 22 33 44 and 50 and the reverse of this thing is very simply is doing 50 reverse 55 44.

33 22 and 11 and finally we are doing this which is again doing a sorting but this time it will arrange the elements in descending order and fortunately the elements are already in descending order so there won't be any change so this will be operation which the this does not change anything so we are getting the same values.

Okay so remember that when we have a reverse equal to true the same sort function returns the like it doesn't return its basically arranges the elements in descending order so we can say uh i think the b b is the correct answer so b is the correct answer now we are coming to the 28th one.

So here we have a list one and list two now both are reference to the same memory location so first time we have made that value 2 is 20 and in the next case we have made in the list 2 as both are pointing to the same memory location so now both list 1 and list 2 when we print they.

Will have a 30 here because they are pointing to the same memory location so in that case we can say answer is d i hope you understood because this is when we write this kind of thing they both point to the same memory location same memory location has two names so any changes will reflected in both the variables.

Okay now this is the 29th one find the output of this one now it's look bit of not so easy to understand so let's see we understand uh this and then we go for the solution now if we see the first this is the nested list we have insert two four files two index means 0 1 2.

So if i take this one here and i put it here so instead of this here insert means it will shift the value so we are getting this value here okay so this is one we have understood okay so this is the what index has done after this insert we have a pop one now pop one means here zero one so this value uh no list one pop one so list one.

Is here this is the list one but in this list one uh because it's a list in this one we have to pop the first element this is zero and one so what happened here we get in this list this.

4 is removed now we are coming to the next one which is the list 2 1 equal to 4 so list 2 means 0 1 2 here the second list uh second element of this list the first value means first index value which is 5 should be 4.

So here if we go this one and we put it here it will be 4 here now finally we have a pop function so for function means uh we are going to remove this five and six from the last value from this list so we have one two three four four.

So let's see uh one two three four four so there's no other value like similar to like this this is the one which should be the answer so let's uh submit this one and this is the correct answer now.

Finally we have come to this one uh where we have list uh and we in this case we have a new list and we are reading each of the list character from this list which means each of the character and uh for carrying e i you you we are going to append this cat so here we see i o.

E i o but here we are printing in a reverse order so it will be o i a uh then o and i so i can see that one which is the c c is the one which is like the correct.

One uh because we have to start with the last character which is o okay so the last o is the one in here i o and it is here uh oh i yes so it will reverse so you can always see whenever you are doing a solution it's always a good thing you can write it down and see what exactly.

Is the way it's working maybe in this mcq portion a minor error may cause you a lose marks so you have to be careful while doing this thing so i hope you have understood uh how this list uh what are the different types of list problems are there which you which may come in the exam.

Which may possibly come in the exam and i hope you have understood what is the reason why one is the answer and why the other is not the answer so uh like my previous videos again like this is the one i have done on the list so i am very soon doing uh more videos on the topics like on the.

Dictionary tuples so that you have some kind of a selective understanding of the topic okay specific understanding of the topic in other words in papers we have all the questions are there so if you uh go through this mcq lecture video i think you will have a.

Clear understanding of the each of the topic and once again share this thing this video with your friends and also subscribe my channel so that i will be motivated to create more videos for you students okay thank you very much

RELATED ARTICLES