Python set pop

The Python set pop method is used to remove a random from a set. It is because the Python set won’t store the items using indexes. You can assign that removed item to a new variable for further reference. The syntax of this set pop method is 

set.pop()

Python set pop example

In this example, we declared a numeric set. Next, we used this set pop function to remove a random set item from the existing set. 

# Python set pop
numeric_set = {150, 250, 350, 450, 550}
 
print('The Original Set : ', numeric_set )
 
numeric_set.pop()

print('The Set after pop: ', numeric_set)
The Original Set :  {450, 550, 150, 250, 350}
The Set after pop:  {550, 150, 250, 350}

This set pop code is the same as the above example. However, this time we are assigning the removed set value to a new variable and printing the same.

TIP: Please refer to the Python set in Python.

numeric_set = {150, 250, 350, 450, 550}
 
print('The Original Set : ', numeric_set )
 
x = numeric_set.pop()
 
print('\npop Item         : ', x)

print('The Set after pop: ', numeric_set)

set pop example output

The Original Set :  {450, 550, 150, 250, 350}

pop Item         :  450
The Set after pop:  {550, 150, 250, 350}

Python pop example 2

In this example, we are using the set pop method to remove all the existing set items one after the other.

numeric_set = {15, 25, 35, 45, 55}
print('The Original Set : ', numeric_set )
 
x = numeric_set.pop()
print('\npop Item         : ', x)
print('The Set after pop: ', numeric_set)
 
y = numeric_set.pop()
print('\npop Item         : ', y)
print('The Set after pop: ', numeric_set)
 
z = numeric_set.pop()
print('\npop Item         : ', z)
print('The Set after pop: ', numeric_set)
 
zz = numeric_set.pop()
print('\npop Item         : ', zz)
print('The Set after pop: ', numeric_set)
The Original Set :  {35, 55, 25, 45, 15}

pop Item         :  35
The Set after pop:  {55, 25, 45, 15}

pop Item         :  55
The Set after pop:  {25, 45, 15}

pop Item         :  25
The Set after pop:  {45, 15}

pop Item         :  45
The Set after pop:  {15}

This time, we are working with Python pop function on the string set.

fruits_set = {'Mango', 'Cherry', 'Apple', 'Kiwi'}
print('The Original Set : ', fruits_set)
 
x = fruits_set.pop()
print('pop Item         : ', x)
print('The Set after pop: ', fruits_set)
 
y = fruits_set.pop()
print('\npop Item         : ', y)
print('The Set after pop: ', fruits_set)
The Original Set :  {'Apple', 'Mango', 'Cherry', 'Kiwi'}
pop Item         :  Apple
The Set after pop:  {'Mango', 'Cherry', 'Kiwi'}

pop Item         :  Mango
The Set after pop:  {'Cherry', 'Kiwi'}

Here, we declared a mixed set. Next, we used the set pop method to remove a random item from a set.

mixed_set = {'Mango', 10, 'Cherry', 20, 'Apple', 30, 'Kiwi'}
print('The Original Set : ', mixed_set)
 
x = mixed_set.pop()
print('\npop Item         : ', x)
print('The Set after pop: ', mixed_set)
 
y = mixed_set.pop()
print('\npop Item         : ', y)
print('The Set after pop: ', mixed_set)
Python set pop method 5