Python Program for Set pop() Method

In the previous article, we have discussed Python Program for Set len() Method
Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given Set = {80, 40, 20, 20, 10, 35, 65}

Output:

The given set is :
{65, 35, 40, 10, 80, 20}
The above given set after poping an item =  {35, 40, 10, 80, 20}

Example2:

Input:

 Given Set = {9, 8, 7, 1, 2, 3, 4, 12, 1}

Output:

The given set is :
{1, 2, 3, 4, 7, 8, 9, 12}
The above given set after poping an item =  {2, 3, 4, 7, 8, 9, 12}

Program for Set pop() Method in Python

set pop() Method:

In python, The pop() method removes an item at random from the set. This is due to the Python set’s inability to store items using indexes. The Python set stores and returns items randomly.

This method returns the item that was removed.

Syntax:

set.pop()

Parameters: This method has no parameter values.

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Apply pop() method to the given set which removes the random element in the given set.
  • Print the above given set after popping an item from the given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {80, 40, 20, 20, 10, 35, 65}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply pop() method to the given set which removes the random element in the given set.
gven_set.pop()
# Print the above given set after popping an item
print("The above given set after poping an item = ", gven_set)

Output:

The given set is :
{65, 35, 40, 10, 80, 20}
The above given set after poping an item =  {35, 40, 10, 80, 20}

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Apply pop() method to the given set which removes the random element in the given set.
  • Print the above given set after popping an item from the given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(), map(), input(), and split() functions.
# Store it in a variable.
gven_set = set(map
(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply pop() method to the given set which removes the random element in the given set.
gven_set.pop()
# Print the above given set after poping an item
print("The above given set after poping an item = ", gven_set)

Output:

Enter some random values separated by spaces = 2 3 4 1 4 2 6 6 3
The given set is :
{1, 2, 3, 4, 6}
The above given set after poping an item = {2, 3, 4, 6}

Well organized and easy to understand tutorials of us containing Python Set Methods Examples will help you work effectively on Python Set Methods.