This method is used to add an element inside a set at any random location, also if the element is already present it does not add the element nor does it throw an error.
This method takes the item (element) as an argument.
- We have taken a set of named any_name and passed some values to it.
- We have used add() method to it and passed mango as an argument.
- We have printed the set.
- We can see Mango inside set at a random location.
- Why we cannot add an element inside the set at the desired location?
Set is a collection data type that is unordered and unindexed, therefore we cannot add an element at the desired location.
- What is the difference between the insert() method of the list and add() method of the set?
Insert() method in list is used to add an element inside list at the desired location, whereas add() method in set is used to add an element at a random location. (More interview questions.)