Python

Python Set Pop Method

Several built-in functions in the Python programming language allow you to remove members from data sequences like sets. The Python set pop() method is a function that removes a random element from a set. In Python, the pop() function returns the element that was randomly removed. The set is immediately updated without the missing piece. The Python interpreter throws a TypeError exception if the set you’re trying to delete an item when the set is empty. For its execution, pop() will only accept one parameter. This input will be used to denote the index of the element that should be popped out of the supplied list. The method of giving an argument to the list is entirely optional; if no argument is supplied, the value ‘-1’ is used as the default. In this case, the default value of -1 denotes the list’s last place. The item is placed at that precise location, and it is also removed from that specific list as a result of the pop() method.

This Python built-in function aids in extracting elements from a set, similar to the technique employed in the implementation of Stack. After deleting a top element from the set, this method returns the detached element. When the Python set pop() method is used, a random element from the set is removed. This means it can pop any piece from any location in the set, regardless of whether it’s in the back or front. Any item in the set can be removed using the pop() method, regardless of its location. After removing a random item from the set, the set pop() method returns the deleted item. Unlike the stack, the set’s random element gets popped. This is quite a popular function in the set, and it has no parameters.

Example 1:

In our first example, we will be using two sets where one contains words and the other numbers. After that, we have applied the pop() method and displayed the updated sets likewise. Here, the word and number set is displayed before and after the pop() function. You will also notice that when we print the set, the values are displayed in accordance with the input data. They appear at random times.

Similarly, when we pop the value, the value is popped at random. We’ve popped values twice and printed the modified sets each time. Though our PC is popping values from the front, yours may be different, and you will obtain different results each time you run this code. Don’t be amazed if you see a different result.

word = {'Apple', 'Banana', 'Juice', 'Red', 'Beans'}
number = {15, 10, 86, 10, 65}

print("Before popping word set is: ", word)
print("Before popping number set is: ", number)

print("word which is popped: ", word.pop())
print("number which is popped: ", number.pop())

print("Updated word set is: ", word)
print("Updated number set is: ", number)

print("word which is popped: ", word.pop())
print("number which is popped: ", number.pop())

print("Updated word set is: ", word)
print("Updated number set is: ", number)

https://lh6.googleusercontent.com/vKmYCyBmyBnMQphLR39r4I71RQzll0N0MeiXuSKcCROy9MaeedDPmYaJPnDEtdZ080aUkfKxbOtZ32bKkRHDAFZ99yx5i46xNzQCjJnxo-yVpiZeHJD7LBrfBEF5mv6DTmu1ouJd

The output of the above code is as follows.

https://lh6.googleusercontent.com/Y56xZLe0JHOiPvQ_wDBz6aE4oQVDnXvMCBVRHSjJj1lAUIRJexcxhdQNzbOFhTTFcGuIq0UjYEBSsltBdnu0Hd47x331ADDAor2_hn7UotS-HuxGngUD5vYRT_qCiUkYaH9Pr5Be

Example 2:

We use the pop() method on an empty set in the second case. We have declared an empty set first. The name of the set in our case is the newest. We’ll now pop a value from the set. The value that is popped is returned by the set pop() method. The value is chosen at random.

myname = {}
print("Name which is popped: ", name.pop())

https://lh6.googleusercontent.com/E_wRymhhl5c4DqogxSF0VC9Y1VnMRG8WqLHX3cIRyMAfGVPL7JBpjVz7gIMX4nYzYySGYc1xv42mtIWJySGlWlvxHTprzxuVABNQlB63WLsrE4-0QOuCoAiyg3dUtaTljrmaiWbl

If the set is empty, a TypeError exception is thrown, as you can perceive from the below screenshot.

https://lh6.googleusercontent.com/vim8UcH7WXZrcydx2tTx-j5tFFQNtRDiETu2_Q63yGuT4x38VfrgiPi_PDX9RQRYBa-wAe60CZ1IoVEU_ZAgKXZmq7bFAxTFgqScQikphw_qLo7Hh0QoS0xQG794lM8tSLVObI5N

Example 3:

In our last example, we will operate pop() on a set that includes numbers and words. To remove a random item from a set, we have declared a mixed set (named mixed_newset), as you can see in the first line of code. Then we have displayed the original set. We have applied the pop method on the mixed_newset, stored the result into the variable abc, and then printed the updated set after applying the pop method. As you can see, the original set is displayed at the start. Then the item which is to be popped is identified, and the remaining item is displayed afterward.

mixed_newset = {'Atomic Blonde', 22, 'Speed',
             19, 'Attack The Block', 37, 'Road House'}
print('Original Set : ', mixed_newset)

abc = mixed_newset.pop()
print('\npop Item  : ', abc)
print('Set after pop: ', mixed_newset)

xyz = mixed_newset.pop()
print('\npop Item  : ', xyz)
print('Set after pop: ', mixed_newset)

https://lh3.googleusercontent.com/-6dI9WREPMAbo0R950wgZYW2DUP0BuZreTzRbXw9jzxbpwmyGO_wTZnRbUY6Y-lR5ta5ncJGM5EbEJZXcVXaR8zrC1IUO-AK9zoh1DPsiuzb6m0XBm8odLgjl_5vimhVi8MAuwGk

When you run the code above, the following result is achieved.

https://lh4.googleusercontent.com/cEEmqYn9IghetRykZc-0rnlZgB4UFgQ78FHu__3Pww8IcnKP4RMMPQO2kldlBdZr9ebH9GXhzVkwrUKGi5em0g595Fm75rC15-zPI5njwj09sCTcGL88ObXpONx6x89YxvZGnv7e

Conclusion:

When the Python set pop() method is used, a random element is removed. This means it can pop any piece from any location in the set, regardless of whether it’s in the back or front. Any item in the set can be removed using the pop() method, regardless of its location. After removing a random item from the set, the set pop() method returns the deleted item. Unlike the stack, the set’s random element gets popped. This is one of the most significant functions in the set, and you must know how to use it. In this brief guide, you have learned how to utilize the set pop. You learned how to utilize the pop() method on an empty set. Last but not least, you learned how to use the pop() method on mixed sets. All of these are explained in detail with examples for your better comprehension.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content