In this Python tutorial, we ‘ll learn about Python set pop method. Here, we’ll see its role and proper usage. We’ll understand its use with the help of multiple Python code examples in order for better understanding. Free source code of these programs will also be provided.
Introduction: Python Set Pop Method
The pop() method is used to remove a random item from an existing set. It means we cannot specify a specific item to be removed from the set, it’s depends on the choice of this method only.
Let’s now make use of this method using practical code examples. But first, let’s understand its syntax in detail.
Syntax of pop() Method
set.pop()
- Python set pop method does not take any arguments.
- Return value of this set is that specific item it’ll remove from the specified Python set.
- If this pop() method is applied on a set that is empty, then a key error exception will be raised.
Understanding Python Set Pop Method (Easy Examples)
We’ve to follow the below examples in order to understand how pop() method works with Python set.
Example 1: pop() Method applied on a Python Set
setX = {11,22,33,44,55} print( setX.pop() ) print( setX ) print( setX.pop() ) print( setX )
Output
33 {11, 44, 22, 55} 11 {44, 22, 55}
- In this code example, we’ve first specified a simple Python set.
- After that, we’ve applied pop() method on it and we can see that it has removed a random item from that set and have returned it. We’ve applied this method again and it has removed another random item from set.
- We can also see that the pop() method has returned the popped item and also it has removed that item from existing set.
Example 2: Python Set Pop Method applied on an Empty Set
setY = set() print( setY.pop() )
Output
KeyError: 'pop from an empty set'
- We’ve created an empty Python set. The set() denotes an empty set.
- Then we’ve applied pop() method on it to see the result.
- We can see that a key error exception has been raised. It means we must have one or more items in our set for pop method to work on it without raising an exception.
Example 3: Return Value of pop() Method
setV = {'aa','bbb',32,67,123,87,4.5,(2,6)} retValue = setV.pop() print('The popped item is ', retValue) print('Updated set is ', setV)
Output
The popped item is 32 Updated set is {67, 4.5, 'aa', (2, 6), 87, 'bbb', 123}
- First, we’ve created a simple set.
- Then we’ve applied pop method on it and have stored the return value(popped item) inside a variable.
- Finally, we’ve printed value of that variable and also the updated set.
- It means this method remove item from the specified set and returns it.
Example 4: Pop method applied on Python List of Sets using For Loop
setLst=[{'a','b',34},{32,67,4,7},{2.1,(27,62)}] newLst=[] for val in setLst: newLst.append(val.pop()) print(newLst)
Output
[34, 32, 2.1]
- In this code example, we’ve created a list of Python sets.
- After that, we’ve used Python for loop which will take each item of list(these items are sets) in every iteration. Click here to learn about the iteration over multiple Python lists simultaneously.
- Inside the body of for loop, we have applied pop method on these sets and have added them to the new list using the append method of that Python list.
- Finally, we’ve displayed the new Python list using Python print function. We can see that this list has items that were popped from the first list of sets.
So this is the proper use of Python set pop method. We’d be happy to see you share it among other Python programmers.
Click here to learn more about Python set methods.
Do feel free to ask questions you may have related to Python set pop method. We’ll be super glad to answer all.
Conclusion
In conclusion, now we’ve a detailed knowledge of how to properly make use of Python set pop method. Please do feel free to share your valuable feedback with us.
Other valuable articles on Python programming are available on this site so don’t hesitate to visit them as well. Thank you for reading this article.