{"id":3538,"date":"2021-04-26T09:46:53","date_gmt":"2021-04-26T04:16:53","guid":{"rendered":"https:\/\/python-programs.com\/?p=3538"},"modified":"2021-11-22T18:43:05","modified_gmt":"2021-11-22T13:13:05","slug":"python-set-remove-vs-discard-vs-pop","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-set-remove-vs-discard-vs-pop\/","title":{"rendered":"Python Set: remove() vs discard() vs pop()"},"content":{"rendered":"
A Python set is a list of things that are not in any particular order. – element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means we can change them after they’ve been formed.<\/p>\n
Unlike other Python sets, the elements of the set have no index, which means we can’t access any element of the set directly using the index. We may, however, print them all together or loop through the collection to get the list of elements.<\/p>\n
Example:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to remove a element 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. Output:<\/strong><\/p>\n Implementing the discard() function if the element is not present in the set:<\/strong><\/p>\n Output:<\/strong><\/p>\n In Python, the built-in method remove() removes an element from a set only if it is already present, similar to how the discard() method removes an element from a set. If the element is missing from the list, an error or exception is thrown.<\/p>\n Implementing the remove() function if the element is present in the set:<\/strong><\/p>\n Output:<\/strong><\/p>\n Implementing the remove() function if the element is not present in the set:<\/strong><\/p>\n Output:<\/strong><\/p>\n This operation takes an arbitrary element from the set and returns it. Implementing the pop() function if the element is present in the set:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n A Python set is a list of things that are not in any particular order. – element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means we can change them after they’ve been formed. Unlike other Python sets, the elements of the set …<\/p>\ngivenset= <\/span>{<\/span>'This'<\/span>, <\/span>'is'<\/span>, <\/span>'BTechGeeks'<\/span>} element='is'\r\n<\/span><\/pre>\n
{'BTechGeeks', 'this'}<\/pre>\n
Remove a element from the Set<\/h2>\n
\n
Method #1:Using discard() function<\/h3>\n
\nImplementing the discard() function if the element is present in the set:<\/strong><\/p>\n# given set\r\ngivenset = {'this', 'is', 'BTechGeeks'}\r\n# given element\r\nelement = 'is'\r\n# removing element which is in set using discard function.\r\ngivenset.discard(element)\r\n# printing the set\r\nprint(givenset)\r\n<\/pre>\n
{'BTechGeeks', 'this'}<\/pre>\n
# given set\r\ngivenset = {'this', 'is', 'BTechGeeks'}\r\n# given element\r\nelement = 'hello'\r\n# removing element which is not in set using discard function.\r\ngivenset.discard(element)\r\n# printing the set\r\nprint(givenset)\r\n<\/pre>\n
{'is', 'this', 'BTechGeeks'}<\/pre>\n
Method #2:Using remove() function<\/h3>\n
# given set\r\ngivenset = {'this', 'is', 'BTechGeeks'}\r\n# given element\r\nelement = 'is'\r\n# removing element which is in set using remove() function.\r\ngivenset.remove(element)\r\n# printing the set\r\nprint(givenset)\r\n<\/pre>\n
{'BTechGeeks', 'this'}<\/pre>\n
# given set\r\ngivenset = {'this', 'is', 'BTechGeeks'}\r\n# given element\r\nelement = 'hello'\r\n# removing element which is not in set using remove() function.\r\ngivenset.remove(element)\r\n# printing the set\r\nprint(givenset)\r\n<\/pre>\n
Traceback (most recent call last):\r\n File \"\/home\/ebe45b71f9f465d547ea9cb944ebe071.py\", line 6, in <module>\r\n givenset.remove(element)\r\nKeyError: 'hello'<\/pre>\n
Method #3:Using pop() function<\/h3>\n
\nIt throws a KeyError if there are no elements to delete.<\/p>\n# given set\r\ngivenset = {'this', 'is', 'BTechGeeks'}\r\n# removing random element which is in set using pop() function.\r\ngivenset.pop()\r\n# printing the set\r\nprint(givenset)\r\n<\/pre>\n
{'is', 'BTechGeeks'}<\/pre>\n
<\/h3>\n
\nRelated Programs<\/strong>:<\/p>\n\n