{"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":"

How to add or append values to a set in Python ?<\/h2>\n

In python add() 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

Set is a data type which stores multiple items in a single variable and the items can be of different types.<\/p>\n

Syntax : set.add(element)<\/pre>\n

It accepts an element as an argument and if that element is not present then it add it o the set.<\/p>\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

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

#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

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

#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

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

#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

By using 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

Suppose we want to add a list, tuple at a time. Then let’s see how it works.<\/p>\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

When we will pass the dictionary inside the 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

When we will pass the dictionary_name.values()<\/code> inside the update()<\/code> method then only values will be added in the set.<\/p>\n

 <\/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>\n

Python: How to add or append values to a set ?<\/span> Read More »<\/a><\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython: How to add or append values to a set ? - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python: How to add or append values to a set ? - Python Programs\" \/>\n<meta property=\"og:description\" content=\"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 … Python: How to add or append values to a set ? Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-23T02:25:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:26:18+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Satyabrata Jena\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/\",\"name\":\"Python: How to add or append values to a set ? - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-23T02:25:22+00:00\",\"dateModified\":\"2023-11-10T06:26:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python: How to add or append values to a set ?\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\"},\"headline\":\"Python: How to add or append values to a set ?\",\"datePublished\":\"2023-10-23T02:25:22+00:00\",\"dateModified\":\"2023-11-10T06:26:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#webpage\"},\"wordCount\":328,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\",\"name\":\"Satyabrata Jena\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"caption\":\"Satyabrata Jena\"},\"url\":\"https:\/\/python-programs.com\/author\/satyabrata\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python: How to add or append values to a set ? - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/","og_locale":"en_US","og_type":"article","og_title":"Python: How to add or append values to a set ? - Python Programs","og_description":"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 … Python: How to add or append values to a set ? Read More »","og_url":"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-23T02:25:22+00:00","article_modified_time":"2023-11-10T06:26:18+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Satyabrata Jena","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#webpage","url":"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/","name":"Python: How to add or append values to a set ? - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-23T02:25:22+00:00","dateModified":"2023-11-10T06:26:18+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python: How to add or append values to a set ?"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd"},"headline":"Python: How to add or append values to a set ?","datePublished":"2023-10-23T02:25:22+00:00","dateModified":"2023-11-10T06:26:18+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#webpage"},"wordCount":328,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-how-to-add-or-append-values-to-a-set\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd","name":"Satyabrata Jena","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","caption":"Satyabrata Jena"},"url":"https:\/\/python-programs.com\/author\/satyabrata\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3625"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=3625"}],"version-history":[{"count":1,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3625\/revisions"}],"predecessor-version":[{"id":3626,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3625\/revisions\/3626"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}