Removing Items | Development | Online Course With Appy Pie Academy

Removing items

Lesson Details:
September 17, 2020

Video Transcription: In this lecture, we will learn about some important methods discard remove pop and clear this video is brought to you by Appy Pie’s Academy, first I will talk about the discard method I am giving the command using the discard method okay how to use it actually discard method is used to delete an element in a set how do we do it first let me create a set with the name values is equal to as you know a set can also be created with the set method so I i'm using the set method and I have created a defined a set with the value 10 20 30 40 50. okay now what I would write is values which is the name of the set dot discard and within parenthesis as an argument I would give the element to be deleted right then I am going to print the sets to see whether this particular element has been deleted or not right so when I would write discard 30 the 30 value will be deleted so I have executed this program and yes as you can see in the output all the values except 30 are getting printed why because it has been discarded so discard method is used to delete a particular element i will show you a very important thing now this time over I have deliberately given a value which is not present in the set that is 37 37 is not in the set now we will see what happens okay i'm going to execute the program and as you can see there was no error there was no error the set was shown as it is without any error message now we come to a method called remove so I am going to give the command using remove method now see remove method is almost the same as the discard method but the difference is if we try to delete if we try to remove a value which is not present in the set it is going to through an error so values dot remove 50. now I have written 50. first I will show you how to remove the value so 50 is present in our set as you can see so let me put these statement print values after discarding 37 let me put it into comments I have executed the program as you can see 50 has been removed 50 has been removed so far so good so there seems to be no difference between discard and remove but now i will show you the difference now what I am going to do is I am going to give a value say say 25 and as you can see 25 does not happen to be present in the set now we will see that an error will occur so i'm going to execute this run and yes yeah you can see this is uh an error has occurred because 25 was not present in the set and we try to remove it using the remove method so that is the basic difference between discard and remove right now I am going to show you another method which is uh um which is pop method now see using pop method so uh see what happens in pop method is it deletes an element it removes an element but which one a random one any it will pick any random number in the set and delete it right so values dot pop it is going to delete an element any element any random element from the set so let me execute the program press the right mouse button on the program click on this run 20 40 10 30 as you can see it has deleted the element 50 right so any random element will be deleted using this pop method uh because we are done with these uh methods I am putting them into comments so now I am going to show you this clear method which is uh used to clear the whole set to remove all the elements of the set so I would write name of the set that is values values dot clear now what it is going to do is it will remove it will delete the whole set okay so after using clear method I am going to print the set so that we can see that all the elements in the set they have been deleted okay so I have executed the program as you can see in the output is it it is showing set and empty parenthesis which is an indication that the set has been completely deleted there are no elements left in the set

loader
Course content