Table of Contents
Definition:
This method is used to delete a random item from the set, also this method returns the deleted item so we can retrieve it using some other variable. It changes the original set.
Syntax
Variable = A.pop()
This method does not take an argument
Working:
Code:
A set is being taken with some values.
- pop() method is applied in a set and deleted value is retrieved in A.
- the values of a set is printed after pop() method
- Deleted value is printed
Output
- Set after pop method
- Deleted value
Interview Questions
- What is the difference between the clear() method and the pop() method?
The clear() method deletes all the elements from the set and returns an empty set, whereas the pop() method only deletes a single random element from the set.
- What is the difference between the discard() method and the pop() method in sets?
The discard method removes the item from the set by directly taking the item as an argument, whereas the pop() method removes the random item from a set.
Quick Links: