{"id":24026,"date":"2021-10-14T09:13:21","date_gmt":"2021-10-14T03:43:21","guid":{"rendered":"https:\/\/python-programs.com\/?p=24026"},"modified":"2021-11-05T17:06:43","modified_gmt":"2021-11-05T11:36:43","slug":"python-program-for-set-discard-method","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-set-discard-method\/","title":{"rendered":"Python Program for Set discard() Method"},"content":{"rendered":"

Python set:<\/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.<\/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 them through the collection.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given set = {'hello', 'this', 'is', 'btechgeeks'} \r\nitem to be discarded = 'hello'<\/pre>\n

Output:<\/strong><\/p>\n

The given set is :\r\n{'is', 'this', 'hello', 'btechgeeks'}\r\nThe given set after discarding the given specific item :\r\n{'is', 'this', 'btechgeeks'}<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given set = {10, 20, 30, 40, 50, 60, 70}\r\nitem to be discarded = 90<\/pre>\n

Output:<\/strong><\/p>\n

The given set is :\r\n{70, 40, 10, 50, 20, 60, 30}\r\nThe given set after discarding the given specific item :\r\n{70, 40, 10, 50, 20, 60, 30}<\/pre>\n

Explanation:<\/strong><\/p>\n

Here there is no 90 in the given set. so, discard() method prints the given set itself without raising an error.\r\nwhereas the remove() method raises an error if item is not present.<\/pre>\n

Program for Set discard() Method in Python<\/h2>\n