Python Program to remove items from the set

PythonServer Side ProgrammingProgramming

We are very much familiar with the term set because in the point of mathematics, we know about set. Set in Python is a data structure equivalent to sets in mathematics. It may consist of various elements; the order of elements in a set is undefined. You can add and delete elements of a set, you can iterate the elements of the set, and you can perform standard operations on sets (union, intersection, difference).

Here set is given we just remove element from the set. here we use pop() method, The pop() is an inbuilt method in Python that is used to pop out or remove the elements one by one from the set.

Example

A = [2, 9, 80, 115, 22, 120]
Output
NEW SET IS ::>
{9, 80, 115, 22, 120}
{80, 115, 22, 120}
{115, 22, 120}
{22, 120}
{120}
Set ()

Algorithm

Step 1: Create a set.
Step 2: Use pop() function. The method pop() removes and returns last object from the list.

Example Code

# Python program to remove elements from set
def remove element(set1):
   print("NEW SET IS ::>")
   while set1:
   set1.pop()
   print(set1)
# Driver Code
set1 = set([22, 120, 130, 115, 80, 9])
remove element(set1)

Output

NEW SET IS ::>
{9, 80, 115, 22, 120}
{80, 115, 22, 120}
{115, 22, 120}
{22, 120}
{120}
set()
raja
Published on 26-Sep-2018 09:38:01
Advertisements