Python Set pop method with Example – Python Tutorial

6 years ago Lalit Bhagtani 0

In this tutorial, we will learn about python set pop method.
Python Set Pop

pop method :

This method is used to remove and return an arbitrary element from the set object. If we try to pop an element from an empty set, It will raises a KeyError.

Syntax : < Set Object >.pop( ) 

Example:

# create set object and assign it to variable s s = {1,2} # pop element from the set object and assign it to variable e e = s.pop() # print the value of variable e print(e) # repeat it one more time.. # pop element from the set object and assign it to variable e e = s.pop() # print the value of variable e print(e) # repeat it one more time.. # pop element from the set object and assign it to variable e e = s.pop() # KeyError is raised as set is empty

References :-

  1. pop method Docs

That’s all for Python Set pop method. If you liked it, please share your thoughts in comments section and share it with others too.

Previous