Set of pythons | pop ()

Python Methods and Functions

Syntax :

 # Pops a random element from S # and returns it. S.pop () 

This is one of the basic set functions and takes no arguments. The return value is the popped item from the set. As soon as an item is retrieved from the set, the set loses the item and is updated to the set without the item.

Examples:

 Input: sets = {1, 2, 3, 4, 5} Output: 1 Updated set is {2, 3, 4, 5} Input: sets = {"ram", "rahim", "ajay", "rishav", "aakash"} Output: rahim Updated set is {'ram',' rishav', 'ajay',' aakash'} 

# Python code to illustrate the pop () method

 

S = { " ram " , " rahim " , "ajay" , "rishav" , "aakash" }

  
# Push three elements and print them

print (S.pop ())

print (S. pop ())

print (S.pop ())

 
# Updated set

print ( "Updated set is" , S)

Output:

 rishav ram rahim Updated set is {'aakash',' ajay'} 

On the other hand, if the set is empty, then TypeError is returned, as shown by the following program.

# Python code to illustrate the pop () method
  # on an empty set

S = {}

 
# Push three elements and print them

print (S.pop ())

 
# Updated set

print ( "Updated set is" , S)

Output:

 No Output 

Error:

 Traceback (most recent call last): File "/home/7c5b1d5728eb9aa0e63b1d70ee5c410e.py", line 6, in print (S.pop ()) TypeError: pop expected at least 1 arguments, got 0 




Tutorials