Python | Remove items from Set
Last Updated :
01 Nov, 2023
In this article, we will try to a way in which the elements can be removed from the set in a sequential manner. Before going into that let’s learn various characteristics of a set.
Examples
Input : set([12, 10, 13, 15, 8, 9])
Output :
{9, 10, 12, 13, 15}
{10, 12, 13, 15}
{12, 13, 15}
{13, 15}
{15}
set()
Set in Python
A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements. Python’s set class represents the mathematical notion of a set. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set.
Methods of removing items from the set
Remove Elements from Set using the 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. The element that is the smallest in the set is removed first followed by removing elements in increasing order. In the following program, the while loop goes onto removing the elements one by one, until the set is empty.
Python3
def Remove(initial_set):
while initial_set:
initial_set.pop()
print (initial_set)
initial_set = set ([ 12 , 10 , 13 , 15 , 8 , 9 ])
Remove(initial_set)
|
Output:
{9, 10, 12, 13, 15}
{10, 12, 13, 15}
{12, 13, 15}
{13, 15}
{15}
set()
Remove items from Set using discard() Method
Initialize a set my_set with the given elements. Enter a loop that runs as long as there are elements in the set my_set. Find the maximum element in the set using the max() function and remove it from the set using the discard() method. Print the updated set after each removal. Exit the loop when the set my_set becomes empty.
Python3
my_set = set ([ 12 , 10 , 13 , 15 , 8 , 9 ])
while my_set:
my_set.discard( max (my_set))
print (my_set)
|
Output
{8, 9, 10, 12, 13}
{8, 9, 10, 12}
{8, 9, 10}
{8, 9}
{8}
set()
The time complexity of the given code is O(n^2), where n is the size of the input set. This is because for each element in the set, the max() function is called, which has a time complexity of O(n). Since there are n elements in the set, the total time complexity becomes O(n^2).
The space complexity of the given code is O(n), where n is the size of the input set. This is because a set of size n is initialized in memory, and the loop runs until all the elements are removed from the set. Therefore, the maximum amount of memory used by the program is proportional to the size of the input set.
Delete Elements from Set Using remove() Method
Initialize set. Use for loop to iterate length of set times. Use the remove method on the first element of the set which is retrieved by the next() method on iter() function.
Python3
my_set = set ([ 12 , 10 , 13 , 15 , 8 , 9 ])
for i in range ( len (my_set)):
my_set.remove( next ( iter (my_set)))
print (my_set)
|
Output
set([9, 10, 12, 13, 15])
set([10, 12, 13, 15])
set([12, 13, 15])
set([13, 15])
set([15])
set([])
Time complexity: O(N) Here N is the length of the set and we are iterating over length of the set.
Space complexity: O(N) N is set lenght.
Please Login to comment...