{"id":788,"date":"2022-07-17T13:56:05","date_gmt":"2023-07-02T00:22:56","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/uncategorized\/python-set\/"},"modified":"2024-03-05T15:03:54","modified_gmt":"2024-03-05T15:03:54","slug":"set-in-python","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/set-in-python\/","title":{"rendered":"Set in Python"},"content":{"rendered":"<div><img decoding=\"async\" class=\"post-images\" title=\"\" src=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655cddc1c40ba52feef294f9\/0-0.jpg\" alt=\"Python Set, python set example\" \/><\/div>\n<h2>Set in <a href=\"https:\/\/www.python.org\/doc\/\">Python<\/a><\/h2>\n<p>A Python Set is a collection of distinct elements that is not ordered. If you have a list and you only want the unique items from it, you can utilize a Python Set. Likewise, if you require only unique items from an input, a Python set can assist in accomplishing that. It is possible to add or remove items from the set. To initialize a set, you can enclose the elements within curly braces. Similar to other sequences, a set can contain elements of different data types. Additionally, you can convert a list into a set by using the set() function. The following example provides an illustration of how to initialize a set.<\/p>\n<pre class=\"post-pre\"><code>#set containing single data-type\r\nset1 = {1, 2, 3, 4, 2, 3, 1}\r\nprint(set1)\r\n\r\n#set containing multiple data-type\r\nset2 = {1, 2, 3, (1, 2, 3), 2.45, \"Python\", 2, 3}\r\nprint(set2)\r\n\r\n#creating a set from a list\r\ntheList = [1, 2, 3, 4, 2, 3, 1]\r\ntheSet = set(theList)\r\nprint(theSet)\r\n<\/code><\/pre>\n<p>The result will be.<\/p>\n<pre class=\"post-pre\"><code>================== RESTART: \/home\/imtiaz\/set1.py ==================\r\nset([1, 2, 3, 4])\r\nset([1, 2, 3, 2.45, 'Python', (1, 2, 3)])\r\nset([1, 2, 3, 4])\r\n&gt;&gt;&gt; \r\n<\/code><\/pre>\n<h3>Including items in a Python set<\/h3>\n<p>In the previous example, we learned the direct initialization of a Python set. If we want to add an element to the set, we can achieve this by using the add() function. However, this function can only add one element at a time. If we want to add iterable elements such as a list or set, we can use the update() function instead. The following example will provide a better understanding of this concept.<\/p>\n<pre class=\"post-pre\"><code>#initialize an empty set\r\ntheSet = set()\r\n\r\n#add a single element using add() function\r\ntheSet.add(1)\r\ntheSet.add(2)\r\ntheSet.add(3)\r\ntheSet.add(2)\r\n#add another data-type\r\ntheSet.add('hello')\r\n\r\n#add iterable elements using update() function\r\ntheSet.update([1,2,4,'hello','world']) #list as iterable element\r\ntheSet.update({1,2,5}) #set as iterable element\r\nprint(theSet)\r\n<\/code><\/pre>\n<p>The result of the given code will be.<\/p>\n<pre class=\"post-pre\"><code>================== RESTART: \/home\/imtiaz\/set_new.py ==================\r\nset([1, 2, 3, 4, 5, 'world', 'hello'])\r\n&gt;&gt;&gt; \r\n<\/code><\/pre>\n<h3>Take out items from a Python set.<\/h3>\n<p>There are two ways to eliminate elements from a Python Set. One of them is the remove() function, and the other is the discard() function. If the element you are attempting to remove is not present in the set, the remove() function will cause an exception. However, the discard() function will not throw any exception in such cases. The code below will demonstrate these functions.<\/p>\n<pre class=\"post-pre\"><code>theSet = {1,2,3,4,5,6}\r\n\r\n#remove 3 using discard() function\r\ntheSet.discard(3)\r\nprint(theSet)\r\n\r\n#call discard() function again to remove 3\r\ntheSet.discard(3) #This won't raise any exception\r\nprint(theSet)\r\n\r\n#call remove() function to remove 5\r\ntheSet.remove(5)\r\nprint(theSet)\r\n\r\n#call remove() function to remove 5 again\r\ntheSet.remove(5) #this would raise exception\r\nprint(theSet) #this won't be printed\r\n<\/code><\/pre>\n<p>You will discover that the result appears similar.<\/p>\n<pre class=\"post-pre\"><code>================== RESTART: \/home\/imtiaz\/set_del.py ==================\r\nset([1, 2, 4, 5, 6])\r\nset([1, 2, 4, 5, 6])\r\nset([1, 2, 4, 6])\r\n\r\nTraceback (most recent call last):\r\n  File \"\/home\/imtiaz\/set_del.py\", line 16, in \r\n    theSet.remove(5) #this would raise exception\r\nKeyError: 5\r\n&gt;&gt;&gt; \r\n<\/code><\/pre>\n<h3>Python operations involving sets.<\/h3>\n<p>You may already have knowledge of mathematical set operations such as union, intersection, and difference. These operations can also be performed using Python sets. In this tutorial, we will acquire the skills to do so.<\/p>\n<h3>Union of Python Sets<\/h3>\n<p>The union operation combines two sets, creating a new set that includes all distinct elements from both sets. For instance, if we have two sets of {1, 2, 3, 4} and {2, 3, 5, 7}, performing the union operation would result in a new set of {1, 2, 3, 4, 5, 7}. This can be achieved by utilizing the union() function.<\/p>\n<h3>Python Set Intersection can be rephrased as finding the common elements between two sets in Python.<\/h3>\n<p>To obtain the shared distinct elements from two sets, we use the intersection operation. For instance, if we have sets {1, 2, 3, 4} and {2, 3, 5, 7}, their intersection would be {2, 3}. This intersection can be carried out using the intersection() function.<\/p>\n<h3>Difference of Sets in Python<\/h3>\n<p>The difference operation in Python compares two sets, A and B, and creates a new set that contains the items from set A which are not present in set B. For example, if we have set A = {1, 2, 3, 4} and set B = {2, 3, 5, 7}, the A &#8211; B operation will result in {1, 4}, while the B &#8211; A operation will result in {5, 7}. The difference operation can be performed using the difference() function in Python. The code below provides an example of how to perform these set operations in Python programming.<\/p>\n<pre class=\"post-pre\"><code>A = {1, 2, 3, 4} #initializing set A\r\nB = {2, 3, 5, 7} #initializing set B\r\n\r\nunion_operation = A.union(B)\r\n\r\nprint(\"A union B :\")\r\nprint(union_operation)\r\n\r\nintersection_operation = A.intersection(B)\r\n\r\nprint(\"A intersection B :\")\r\nprint(intersection_operation)\r\n\r\ndifference_operation = A.difference(B)\r\n\r\nprint(\"A-B :\")\r\nprint(difference_operation)\r\n\r\ndifference_operation = B.difference(A)\r\nprint(\"B-A :\")\r\nprint(difference_operation)\r\n<\/code><\/pre>\n<p>You will receive a result in this format.<\/p>\n<pre class=\"post-pre\"><code>================== RESTART: \/home\/imtiaz\/set_op.py ==================\r\nA union B :\r\nset([1, 2, 3, 4, 5, 7])\r\nA intersection B :\r\nset([2, 3])\r\nA-B :\r\nset([1, 4])\r\nB-A :\r\nset([5, 7])\r\n&gt;&gt;&gt; \r\n<\/code><\/pre>\n<p>That concludes our discussion on Python Sets. We hope you have gained a good understanding. If you have any additional questions or need further assistance, please feel free to leave a comment. We will be glad to help. Source: Official Documentation<\/p>\n<p>&nbsp;<\/p>\n<p>Other Python tutorials<\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/python-breakpoint-rewrite-this-utilizing-pure-natural-language-please-provide-one-alternative-introduce-a-stopping-point-in-python-using-the-breakpoint-function\/\" target=\"_blank\" rel=\"noopener\">breakpoint function in Python<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/python-functions-ord-and-chr\/\" target=\"_blank\" rel=\"noopener\">The Python functions ord() and chr()<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/substring-in-python-refers-to-extracting-a-smaller-portion-of-a-string\/\" target=\"_blank\" rel=\"noopener\">Python Substring refers to extracting a smaller portion of a string.<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Set in Python A Python Set is a collection of distinct elements that is not ordered. If you have a list and you only want the unique items from it, you can utilize a Python Set. Likewise, if you require only unique items from an input, a Python set can assist in accomplishing that. It [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-788","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Set in Python - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"A Python Set is a collection of distinct elements that is not ordered. If you have a list and you only want the unique items from it,\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/set-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Set in Python\" \/>\n<meta property=\"og:description\" content=\"A Python Set is a collection of distinct elements that is not ordered. If you have a list and you only want the unique items from it,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/set-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-02T00:22:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-05T15:03:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655cddc1c40ba52feef294f9\/0-0.jpg\" \/>\n<meta name=\"author\" content=\"Noah Thompson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Noah Thompson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/set-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/set-in-python\/\"},\"author\":{\"name\":\"Noah Thompson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a\"},\"headline\":\"Set in Python\",\"datePublished\":\"2023-07-02T00:22:56+00:00\",\"dateModified\":\"2024-03-05T15:03:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/set-in-python\/\"},\"wordCount\":614,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/set-in-python\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/set-in-python\/\",\"name\":\"Set in Python - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2023-07-02T00:22:56+00:00\",\"dateModified\":\"2024-03-05T15:03:54+00:00\",\"description\":\"A Python Set is a collection of distinct elements that is not ordered. If you have a list and you only want the unique items from it,\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/set-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/set-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/set-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Set in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a\",\"name\":\"Noah Thompson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g\",\"caption\":\"Noah Thompson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/noahthompson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Set in Python - Blog - Silicon Cloud","description":"A Python Set is a collection of distinct elements that is not ordered. If you have a list and you only want the unique items from it,","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:\/\/www.silicloud.com\/blog\/set-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Set in Python","og_description":"A Python Set is a collection of distinct elements that is not ordered. If you have a list and you only want the unique items from it,","og_url":"https:\/\/www.silicloud.com\/blog\/set-in-python\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2023-07-02T00:22:56+00:00","article_modified_time":"2024-03-05T15:03:54+00:00","og_image":[{"url":"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655cddc1c40ba52feef294f9\/0-0.jpg"}],"author":"Noah Thompson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Noah Thompson","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/set-in-python\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/set-in-python\/"},"author":{"name":"Noah Thompson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a"},"headline":"Set in Python","datePublished":"2023-07-02T00:22:56+00:00","dateModified":"2024-03-05T15:03:54+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/set-in-python\/"},"wordCount":614,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/set-in-python\/","url":"https:\/\/www.silicloud.com\/blog\/set-in-python\/","name":"Set in Python - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2023-07-02T00:22:56+00:00","dateModified":"2024-03-05T15:03:54+00:00","description":"A Python Set is a collection of distinct elements that is not ordered. If you have a list and you only want the unique items from it,","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/set-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/set-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/set-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Set in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a","name":"Noah Thompson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g","caption":"Noah Thompson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/noahthompson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/788","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=788"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/788\/revisions"}],"predecessor-version":[{"id":1674,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/788\/revisions\/1674"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}