Python set pop

The Python pop method is used to remove a random from a set. It is because the 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 Python set pop method is

set.pop()

Python set pop Function example

The pop method removes the last item from a given one. As we all know, sets are not in any particular order. So, you never know which item was removed from it. In this example, we declared a numeric. Next, we used this Python pop function to remove a random set item from the existing one. 

numeric = {150, 250, 350, 450, 550}
 
print('The Original : ', numeric )
 
numeric.pop()

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

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

TIP: Please refer to the set in Python.

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

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

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

How to use set pop to remove all items?

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

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

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

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

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

pop Item      :  45
The Set after :  {15}

How to pop items in a string set?

We are working with the pop function on the string set this time.

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

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

Although it is not worth it, I am giving one example. So, you get the idea.

a = {1, 2, 3, 4, 5, 6, 7, 8, 9}
print("Old Items = ", a)

a.discard(7)
a.discard(4)
print("New Set Items = ", a)

Fruits = {'apple', 'Mango', 'orange', 'banana', 'cherry','kiwi'}
print("\nOld Items = ", Fruits)

Fruits.discard('Mango')
print("New Items = ", Fruits)
Old Items = {1, 2, 3, 4, 5, 6, 7, 8, 9}
New Items = {1, 2, 3, 5, 6, 8, 9}

Old Items = {'Mango', 'cherry', 'banana', 'apple', 'kiwi', 'orange'}
New Items = {'cherry', 'banana', 'apple', 'kiwi', 'orange'}

How to pop items from the mixed set?

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

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