Python Set pop() Method - GeeksforGeeks

Python Set pop() Method

Last Updated : 06 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python set pop() removes any random element from the set and returns the removed element. In this article, we will see about the Python set pop() method.

Example

Input: {9, 1, 0}
Output: {9, 1}
Explanation: By using set pop() method, a random element 0 is removed from the set and remaining set is returned.

Python Set pop() Syntax

Syntax: set_obj.pop()

Parameter: set.pop() doesn’t take any parameter.

Return: Returns the popped element from the set

Set pop() Method in Python

Python Set pop() is a method in Python used to remove and return any random element from the set. As we all know, Sets are an unordered collection of unique elements, so there’s no guarantee which element will be removed and returned by the pop() method. If the set is empty, calling pop() will raise a KeyError.

Python Set pop() Method Example

Example 1: In this example, we are using the Python Set pop() method to pop any random element from the set and then print the remaining set.

Python3




s1 = {9, 1, 0}
s1.pop()
print(s1)


Output

{9, 1}

Example 2: In this example, we are using Python Set pop() to pop 3 elements from a set and then print the remaining set.

Python3




s1 = {1, 2, 3, 4}
print("Before popping: ",s1)
s1.pop()
s1.pop()
s1.pop()
 
print("After 3 elements popped, s1:", s1)


Output

Before popping:  {1, 2, 3, 4}
After 3 elements popped, s1: {4}

Exceptions while using Python Set pop() Method

In Python, TypeError is returned if the set is empty and we try to pop out the elements from the set. In this example, pop() method is used in an empty set to pop out the element but TypeError is returned as the result.

Python3




S = {}
 
# popping an element
print(S.pop())
 
print("Updated set is", S)


Output:

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


Previous Article
Next Article

Similar Reads

List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
Some of the list methods are mentioned in set 1 below List Methods in Python | Set 1 (in, not in, len(), min(), max()…) More methods are discussed in this article. 1. del[a : b] :- This method deletes all the elements in range starting from index 'a' till 'b' mentioned in arguments. 2. pop() :- This method deletes the element at the position mentio
4 min read
Python Dictionary pop() Method
Python dictionary pop() method removes and returns the specified element from the dictionary. Example: C/C++ Code # inializing dictionary student student = {"rahul":7, "Aditya":1, "Shubham":4} # priting original dictionary print(student) # using dictionary pop suspended = student.pop("rahul") # checking key o
4 min read
Python List pop() Method
Python list pop() function removes elements at a specific index from the list. Example Python Code fruits.pop() print(fruits) Output: ['apple', 'mango']Python List pop() Syntaxlist_name.pop(index)Parameterindex (optional) - The value at the index is popped out and removed. If the index is not given, then the last element is popped out and removed.R
2 min read
Python | Pandas Dataframe.pop()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.Pandas Pop() method is common in most of the data structures but pop() method is a little bit different from the rest. In a stack, pop do
2 min read
Python | Pandas Series.pop()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.pop() function returns the item corresponding to the passed index label. It als
2 min read
Wand push() and pop() in Python
We can use ImageMagick's internal graphic context stack to manage different styles and operations in Wand. There are total four push functions for context stack. push()push_clip_path()push_defs()push_pattern() push() function is used to grow context stack and pop() is another function and used to restore stack to previous push. Syntax : # for push(
2 min read
What Is Difference Between Del, Remove and Pop on Python Lists?
In python del is a keyword and remove(), pop() are in-built methods. The purpose of these three are same but the behavior is different remove() method delete values or object from the list using value and del and pop() deletes values or object from the list using an index. del Keyword: The del keyword delete any variable, list of values from a list
3 min read
How to create a pop up message when a button is pressed in Python - Tkinter?
In this article, we will see how to create a button and how to show a popup message when a button is pressed in Python. Tkinter is a standard Python Package for creating GUI applications. Tkinter may be a set of wrappers that implement the Tk widgets as Python classes. Python, when combined with Tkinter, provides a quick and straightforward way to
2 min read
IndexError: pop from Empty List in Python
The IndexError: pop from an empty list is a common issue in Python, occurring when an attempt is made to use the pop() method on a list that has no elements. This article explores the nature of this error, provides a clear example of its occurrence, and offers three practical solutions to handle it effectively. Here, we will see how to fix Pop From
3 min read
IndexError: pop from Empty Deque in Python
In Python, the IndexError: pop from an empty deque is an error that occurs when trying to use the pop() method on an empty deque. A deque (double-ended queue) is a versatile data structure that allows efficient addition and removal of elements from both ends. This article explores the causes of the error, provides examples of its occurrence, and of
4 min read
Article Tags :
Practice Tags :