{"id":3625,"date":"2023-10-23T07:55:22","date_gmt":"2023-10-23T02:25:22","guid":{"rendered":"https:\/\/python-programs.com\/?p=3625"},"modified":"2023-11-10T11:56:18","modified_gmt":"2023-11-10T06:26:18","slug":"python-how-to-add-or-append-values-to-a-set","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/","title":{"rendered":"Python: How to add or append values to a set ?"},"content":{"rendered":"
In python Set is a data type which stores multiple items in a single variable and the items can be of different types.<\/p>\n It accepts an element as an argument and if that element is not present then it add it o the set.<\/p>\n As set contains only unique elements, so duplicate elements so it will not not add the element again means set has no effect.<\/p>\n Let’s see an example program for it.<\/p>\n Set contains immutable elements. So, int, strings, bytes, frozen sets, tuples can be added as these are immutable object. But we can not add a list as it is mutable object.<\/p>\n Let’s see an example program for it how we can add a tuple to the set using add function.<\/p>\n Suppose we are trying to add a list as it is mutable object. It will give Type error.<\/p>\n Let’s see an example program for it.<\/p>\n By using Suppose we want to add a list, tuple at a time. Then let’s see how it works.<\/p>\n When we will pass the dictionary inside the When we will pass the <\/p>\n","protected":false},"excerpt":{"rendered":" How to add or append values to a set in Python ? In python add() method or append() method is used to simply add an item in set, tuple, dictionary etc.. but this method doesn’t add an element\u00a0 if the entered element already present before it. Adding a single element to the set : Set …<\/p>\nadd() method<\/code> or
append() method<\/code> is used to simply add an item in set, tuple, dictionary etc.. but this method doesn’t add an element\u00a0 if the entered element already present before it.<\/p>\n
Adding a single element to the set :<\/strong><\/h3>\n
Syntax : set.add(element)<\/pre>\n
Add a value to the set using add() function :<\/strong><\/h3>\n
#program :\r\n\r\n#Set initialized\r\nmy_set = {1, 2, \"Hello\", 3, \"Hi\"} \r\n# Adding a single element in set \r\nmy_set.add(5) \r\nprint(my_set)\r\n<\/pre>\n
Output :\r\n{1,2,3,5'Hello','Hi'}<\/pre>\n
Adding a duplicate element to the set :<\/h3>\n
#program :\r\n\r\n#Set initialized\r\nmy_set = {1, 2, \"Hello\", 3, \"Hi\"} \r\n# Adding 'Hello' in set \r\nmy_set.add(\"Hello\") \r\nprint(my_set)\r\n<\/pre>\n
Output :\r\n{1,2,3,5'Hello','Hi'}<\/pre>\n
Adding immutable objects to the set using add() function :<\/h3>\n
#Program :\r\n\r\n#Set initialized\r\nmy_set = {1, 2, \"Hello\", 3, \"Hi\"}\r\n# Adding a tuple in set\r\nmy_set.add((4, 5))\r\nprint(my_set)<\/pre>\n
Output :\r\n{1, 2, (4,5),\"Hello\", 3, \"Hi\"}<\/pre>\n
Adding a mutable object to set will give TypeError :<\/h4>\n
#Program :\r\n\r\n#Set initialized\r\nmy_set = {1, 2, \"Hello\", 3, \"Hi\"}\r\n# Adding a tuple in set\r\nmy_set.add([21, 31])\r\nprint(my_set)<\/pre>\n
Output :\r\nTypeError: unhashable type: <\/span>'list'<\/span><\/pre>\n
Adding multiple elements to the set :<\/h3>\n
update()<\/code> method we can add elements to the set.<\/p>\n
Syntax : set.<\/span>update<\/span>(<\/span>*args<\/span>)<\/span><\/pre>\n
#Program :\r\n\r\n#Set initialized\r\nmy_set = {1, 2, \"Hello\", 3, \"Hi\"}\r\n# Adding 10,20,30 in set\r\n#update() method is used\r\nmy_set.update((10, 20, 30))\r\nprint(my_set)<\/pre>\n
Output :\r\n{1,2,3,5'Hello','Hi',10,20,30}<\/pre>\n
Adding elements from multiple sequences to the set :<\/h3>\n
#Program :\r\n\r\n#Set initialized\r\nmy_set = {1, 2, \"Hello\", 3, \"Hi\"}\r\n\r\n#list initialized\r\nmy_list= [10, 20, 3]\r\n#tuple initialized\r\nmy_tuple = (31, 32)\r\n#update() method is used\r\nmy_set.update(my_list,my_tuple)\r\nprint(my_set)<\/pre>\n
Output :\r\n{1,2,3,5'Hello','Hi',10,20,30,31,32}<\/pre>\n
Adding dictionary keys to the set :<\/h3>\n
#Program :\r\n\r\n#Set initialized\r\nmy_set = set()\r\n#dictionary initialized\r\nmy_dict = {'Satya': 11, 'Omm': 12, 'Nitya': 15}\r\n#only keys ill be added\r\nmy_set.update(my_dict)\r\nprint(my_set)<\/pre>\n
Output :\r\n{'Satya','Omm','Nitya'}<\/pre>\n
update()<\/code> method then only key will be added in the set.<\/p>\n
Adding dictionary values to the set :<\/h3>\n
#Program :\r\n\r\n#Set initialized\r\nmy_set = set()\r\n#dictionary initialized\r\nmy_dict = {'Satya': 10, 'Omm': 20, 'Nitya': 30}\r\n#only keys ill be added\r\nmy_set.update(my_dict.values())\r\nprint(my_set)<\/pre>\n
Output :\r\n{10,20,30}<\/pre>\n
dictionary_name.values()<\/code> inside the
update()<\/code> method then only values will be added in the set.<\/p>\n