Table of Contents
The pop() method removes an arbitrary element from the set and returns the element removed.
The syntax of pop()
for sets is:
set.pop()
1. pop() Parameters
The pop()
method doesn’t take any arguments.
2. Return Value from pop()
The pop()
method returns an arbitrary (random) element from the set. Also, the set is updated and will not contain the element (which is returned).
If the set is empty, TypeError
exception is raised.
3. Example: How pop() works for Python Sets?
A ={'a', 'b', 'c', 'd'} print('Return Value is', A.pop()) print('A = ', A)
When you run the program, we got the following output
Return Value is d A = {'a', 'b', 'c'}
Note: You may get different output as
pop()
returns and removes a random element.
Related posts:
Python List sort()
Python Program to Check if a Number is Positive, Negative or 0
Python pass statement
Python memoryview()
Python Modules
Python Program to Transpose a Matrix
Python eval()
Python if...else Statement
Python Program to Find Factorial of Number Using Recursion
Python String isdigit()
Python hasattr()
Python Errors and Built-in Exceptions
Python Program to Get the Class Name of an Instance
Python set()
Python Program to Find ASCII Value of Character
Python repr()
Python setattr()
Python bytes()
Python String format()
Python locals()
Python List append()
Python break and continue
Python Program to Randomly Select an Element From the List
Python dict()
Python Program to Remove Duplicate Element From a List
Python Program to Create a Countdown Timer
Python Objects and Classes
Python Program to Multiply Two Matrices
Python Statement, Indentation and Comments
Python Program to Safely Create a Nested Directory
Node.js vs Python for Backend Development
Python Inheritance