{"id":3744,"date":"2023-10-24T16:23:06","date_gmt":"2023-10-24T10:53:06","guid":{"rendered":"https:\/\/python-programs.com\/?p=3744"},"modified":"2023-11-10T11:57:06","modified_gmt":"2023-11-10T06:27:06","slug":"python-remove-all-elements-from-set-using-clear-difference_update-discard","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-remove-all-elements-from-set-using-clear-difference_update-discard\/","title":{"rendered":"Python: Remove All Elements from Set using Clear()\/Difference_Update()\/Discard()"},"content":{"rendered":"
Python set is a list of items that are not ordered. \u2013 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.<\/p>\n
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 through the collection.<\/p>\n
Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to remove all the elements from the set some of them are:<\/p>\n In Python, the built-in method discard() removes an element from a collection only if it is already present. If the element is missing from the list, no error or exception is thrown, and the original set is printed.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The clear() method clears the set of all elements.<\/p>\n syntax:<\/strong><\/p>\n parameters: <\/strong><\/p>\n Return :<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The Set class in Python has a function called difference update() that takes a sequence as an argument and removes all of the elements from the set. let us use this method to remove all the elements from the set<\/p>\n We can pass given set as parameter for the difference_update() function<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Python set is a list of items that are not ordered. \u2013 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 …<\/p>\ngivenset= <\/span>{<\/span>'this'<\/span>, <\/span>'is'<\/span>, <\/span>'BTechGeeks'<\/span>}<\/span><\/pre>\n
set()<\/pre>\n
Delete all the elements from the set<\/h2>\n
\n
Method #1:Using discard() function<\/h3>\n
\n
# given set\r\ngivenset = {'this', 'is', 'BTechGeeks'}\r\n\r\n# using for loop to traverse the set\r\nfor element in list(givenset):\r\n # remove that element from set\r\n givenset.discard(element)\r\n# print the set\r\nprint(givenset)\r\n<\/pre>\n
set()<\/pre>\n
Method #2:Using clear() function<\/h3>\n
given_set.clear()<\/pre>\n
The clear() method accepts no parameters.<\/pre>\n
The clear() method does not return a value.<\/pre>\n
# given set\r\ngivenset = {'this', 'is', 'BTechGeeks'}\r\n\r\n# using clear()\r\ngivenset.clear()\r\n# print the set\r\nprint(givenset)\r\n<\/pre>\n
set()<\/pre>\n
Method #3:Using difference_update() method<\/h3>\n
# given set\r\ngivenset = {'this', 'is', 'BTechGeeks'}\r\n# using difference_update()\r\ngivenset.difference_update(givenset)\r\n# print the set\r\nprint(givenset)\r\n<\/pre>\n
set()<\/pre>\n
\n