{"id":23994,"date":"2021-10-14T09:13:42","date_gmt":"2021-10-14T03:43:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=23994"},"modified":"2021-11-05T16:48:59","modified_gmt":"2021-11-05T11:18:59","slug":"python-program-for-set-remove-method","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-set-remove-method\/","title":{"rendered":"Python Program for Set remove() Method"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Set min() Method<\/a> 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 Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n set remove() Method:<\/strong><\/p>\n In Python, the built-in method remove() removes the element from the set only if the element is present in the set, similarly to the discard() method, but If the element is not found in the set, an error or exception is thrown.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values :\u00a0<\/strong><\/p>\n item:<\/strong> This is required compulsorily.The item to look for and remove<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Python Set Methods Examples: Understanding Python Set Methods without any practice is a bit difficult but going through this Python Set Methods Examples<\/a> tutorial will definitely aid programmers to learn easily & efficiently.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python Program for Set min() Method Python set: 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 …<\/p>\n
\nPython set:<\/strong><\/p>\nGiven set = {'hello', 'this', 'is', 'btechgeeks'}\r\nitem to be removed = 'hello'<\/pre>\n
The given set is :\r\n{'hello', 'btechgeeks', 'is', 'this'}\r\nThe given set after removing the given specific item :\r\n{'btechgeeks', 'is', 'this'}<\/pre>\n
Given set = {10, 30, 50, 20, 30, 40, 60}\r\nitem to be removed = 10<\/pre>\n
The given set is :\r\n{40, 10, 50, 20, 60, 30}\r\nThe given set after removing the given specific item :\r\n{40, 50, 20, 60, 30}<\/pre>\n
Program for Set remove() Method in Python<\/h2>\n
\n
set.remove(item)<\/pre>\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the set as static input and initialize it with some random values.\r\n# Store it in a variable.\r\ngven_set = {'hello', 'this', 'is', 'btechgeeks'}\r\n# Print the above-given set.\r\nprint(\"The given set is :\")\r\nprint(gven_set)\r\n# Give the item to be removed as static input.\r\n# Store it in another variable.\r\nremve_item = 'hello'\r\n# Apply remove() method to the given set which removes the above given specific item\r\n# in the given set.\r\ngven_set.remove(remve_item)\r\n# Print the above-given set after removing the given specific item.\r\nprint(\"The given set after removing the given specific item :\")\r\nprint(gven_set)\r\n<\/pre>\n
The given set is :\r\n{'hello', 'btechgeeks', 'is', 'this'}\r\nThe given set after removing the given specific item :\r\n{'btechgeeks', 'is', 'this'}<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the set as user input using the set(),map(), input(), and split() functions.\r\n# Store it in a variable.\r\ngven_set = set(map\r\n(int,input(\"Enter some random values separated by spaces = \").split()))\r\n# Print the above-given set.\r\nprint(\"The given set is :\")\r\nprint(gven_set)\r\n# Give the item to be removed as user input using the int(input()) function.\r\n# Store it in another variable.\r\nremve_item= int(input(\"Enter some random number = \"))\r\n# Apply remove() method to the given set which removes the above given specific item in the given set.\r\ngven_set.remove(remve_item)\r\n# Print the above-given set after removing the given specific item.\r\nprint(\"The given set after removing the given specific item :\")\r\nprint(gven_set)\r\n<\/pre>\n
Enter some random values separated by spaces = 10 30 50 20 30 40 60\r\nThe given set is :\r\n{40, 10, 50, 20, 60, 30}\r\nEnter some random number = 10\r\nThe given set after removing the given specific item :\r\n{40, 50, 20, 60, 30}<\/pre>\n
\n