{"id":45,"date":"2023-03-30T04:56:53","date_gmt":"2023-03-30T04:56:53","guid":{"rendered":"http:\/\/www.programminginpython.com\/2023\/03\/30\/set-operations-in-python\/"},"modified":"2023-05-21T19:09:30","modified_gmt":"2023-05-21T19:09:30","slug":"set-operations-python","status":"publish","type":"post","link":"https:\/\/www.programminginpython.com\/set-operations-python\/","title":{"rendered":"Set Operations in Python"},"content":{"rendered":"<p>Hello everyone, welcome back to <a href=\"http:\/\/programminginpython.com\" target=\"_blank\" rel=\"noopener noreferrer\">programminginpython.com<\/a>. Here in this post, I am going to discuss another data type in Python called <code>SET<\/code>, which is an unordered collection of data that is iterable and has no duplicate elements. The set in Python represents the mathematical notation of a set.<\/p>\n<p>A set can be simply initialized as <code>example = set()<\/code>, here example is the name of the set, and can also put some elements in it like <code>example = set(['one', 2, 'three']) <\/code>and here in <code>set<\/code> datatype, the order of the elements is not preserved, which means lists and tuples where the order of the elements is checked for indexing.<\/p>\n<p>Now I will perform some of the operations on sets. So let\u2019s get started.<\/p>\n<div class=\"yrc-shell-cover yrc-single\" data-yrc-uid=\"69e0d6175a783\" data-yrc-channel=\"{&quot;meta&quot;:{&quot;user&quot;:&quot;Programming In Python&quot;,&quot;channel&quot;:&quot;UCLBlXUMLYLxopRljNdLXOeQ&quot;,&quot;key&quot;:&quot;yrc_1683138754&quot;,&quot;apikey&quot;:&quot;AIzaSyC3vU6rFyzsO0shrRsAWmjuSiuWYTQhafc&quot;,&quot;cache&quot;:&quot;1440&quot;,&quot;channel_uploads&quot;:&quot;UULBlXUMLYLxopRljNdLXOeQ&quot;,&quot;onlyonce&quot;:&quot;&quot;,&quot;tag&quot;:&quot;&quot;,&quot;per_page&quot;:&quot;24&quot;,&quot;maxv&quot;:&quot;0&quot;,&quot;consent&quot;:{&quot;ask&quot;:&quot;&quot;,&quot;url&quot;:&quot;&quot;},&quot;ads&quot;:&quot;1&quot;,&quot;uid&quot;:&quot;69e0d6175a783&quot;,&quot;nocookie&quot;:&quot;&quot;,&quot;single&quot;:true},&quot;style&quot;:{&quot;colors&quot;:{&quot;item&quot;:{&quot;background&quot;:&quot;inherit&quot;},&quot;button&quot;:{&quot;background&quot;:&quot;#333&quot;,&quot;color&quot;:&quot;#fff&quot;},&quot;color&quot;:{&quot;text&quot;:&quot;#fff&quot;,&quot;link&quot;:&quot;inherit&quot;,&quot;menu&quot;:&quot;#000&quot;,&quot;meta&quot;:&quot;inherit&quot;}},&quot;theme&quot;:{&quot;videos&quot;:{&quot;style&quot;:&quot;__grid&quot;,&quot;thumb&quot;:[&quot;large&quot;,&quot;open&quot;],&quot;desc&quot;:&quot;&quot;,&quot;carousel&quot;:{&quot;thumbs&quot;:&quot;4&quot;,&quot;thumbs_to_slide&quot;:&quot;2&quot;,&quot;spacing&quot;:&quot;10&quot;},&quot;carousel_nav&quot;:{&quot;modifier&quot;:&quot;__sides&quot;,&quot;position&quot;:&quot;left-none&quot;,&quot;location&quot;:&quot;prepend&quot;,&quot;background&quot;:&quot;#fff&quot;,&quot;color&quot;:&quot;#000&quot;,&quot;font_size&quot;:&quot;2&quot;,&quot;border_radius&quot;:&quot;0&quot;}},&quot;a&quot;:&quot;1&quot;},&quot;fit&quot;:&quot;false&quot;,&quot;playlists&quot;:&quot;true&quot;,&quot;uploads&quot;:&quot;true&quot;,&quot;player_mode&quot;:&quot;1&quot;,&quot;truncate&quot;:&quot;&quot;,&quot;banner&quot;:&quot;true&quot;,&quot;thumb_margin&quot;:&quot;8&quot;,&quot;play_icon&quot;:&quot;hover&quot;,&quot;youtube_play_icon&quot;:&quot;&quot;,&quot;thumb_image_size&quot;:&quot;medium&quot;,&quot;default_tab&quot;:&quot;uploads&quot;,&quot;sticky&quot;:{&quot;enable&quot;:&quot;&quot;,&quot;width&quot;:&quot;400&quot;,&quot;position&quot;:&quot;bottom-right&quot;,&quot;only_above&quot;:&quot;768&quot;,&quot;margin&quot;:&quot;12&quot;},&quot;player&quot;:{&quot;show_desc&quot;:&quot;&quot;,&quot;show_meta&quot;:&quot;&quot;},&quot;menu&quot;:&quot;1&quot;,&quot;rating_style&quot;:&quot;NaN&quot;,&quot;rtl&quot;:false}}\" data-yrc-setup=\"\"><\/div>\r\n\t\t\t<script data-cfasync=\"false\" type=\"text\/javascript\">\r\n\t\t\t\tif( !window.YRC ) var YRC = {Data:{}};\r\n\t\t\t\tYRC.Data[\"69e0d6175a783\"] = {\"video\":\"sDyOLjixXT0\"};\r\n\t\t\t\t(function(){\r\n\t\t\t\t\tif(!YRC.loaded){\r\n\t\t\t\t\t    YRC.loaded = true;\r\n\t\t\t\t\t\tfunction YRC_Loader(){\r\n\t\t\t\t\t\t\t\/\/YRC.loaded = true;\r\n\t\t\t\t\t\t\tYRC.is_pro = false;\r\n\t\t\t\t\t\t\tYRC.is_pb = false;\r\n\t\t\t\t\t\t\tYRC.lang = {\"form\":{\"Videos\":\"Videos\",\"Playlists\":\"Playlists\",\"Search\":\"Search\",\"Loading\":\"Loading\",\"more\":\"more\",\"Nothing_found\":\"Nothing found\",\"Prev\":\"Previous\",\"Next\":\"Next\",\"consent_statement\":\"Allow cookies?\",\"consent_button\":\"Allow\",\"consent_privacy_policy\":\"Privacy policy\"},\"fui\":{\"sort_by\":\"Sort by\",\"relevant\":\"Relevant\",\"latest\":\"Latest\",\"liked\":\"Liked\",\"title\":\"Title\",\"views\":\"Views\",\"duration\":\"Duration\",\"any\":\"Any\",\"_short\":\"Short\",\"medium\":\"Medium\",\"_long\":\"Long\",\"uploaded\":\"Uploaded\",\"all_time\":\"All time\",\"live_now\":\"Live Now\",\"today\":\"Today\",\"ago\":\"ago\",\"last\":\"Last\",\"day\":\"day\",\"days\":\"days\",\"week\":\"week\",\"weeks\":\"weeks\",\"month\":\"month\",\"months\":\"months\",\"year\":\"year\",\"years\":\"years\",\"older\":\"Older\",\"show_more\":\"Show More\",\"show_less\":\"Show Less\",\"reply\":\"REPLY\",\"view_replies\":\"View replies\",\"write_comment\":\"Write comment...\",\"billion\":\"B\",\"million\":\"M\",\"thousand\":\"K\",\"max_plain_number\":1000,\"wplocale\":\"en_US\"}};\r\n\t\t\t\t\t\t\tYRC.is_admin = false;\r\n\t\t\t\t\t\t\tvar script = document.createElement(\"script\");\r\n\t\t\t\t\t\t\t\tscript.setAttribute(\"data-cfasync\", \"false\");\r\n\t\t\t\t\t\t\t\tscript.setAttribute(\"type\", \"text\/javascript\");\r\n\t\t\t\t\t\t\t\tscript.src = \"https:\/\/www.programminginpython.com\/wp-content\/plugins\/yourchannel\/js\/yrc.js?1.2.9\";\r\n\t\t\t\t\t\t\t\tscript.id = \"yrc-script\";\r\n\t\t\t\t\t\t\t\tdocument.querySelector(\"head\").appendChild(script);\r\n\t\t\t\t\t\t\tvar style = document.createElement(\"link\");\r\n\t\t\t\t\t\t\t\tstyle.rel = \"stylesheet\";\r\n\t\t\t\t\t\t\t\tstyle.href = \"https:\/\/www.programminginpython.com\/wp-content\/plugins\/yourchannel\/css\/style.css?1.2.9\";\r\n\t\t\t\t\t\t\t\tstyle.type = \"text\/css\";\r\n\t\t\t\t\t\t\t\tdocument.querySelector(\"head\").appendChild(style);\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\tif(window.jQuery){YRC_Loader();}else { var yrctimer2324 = window.setInterval(function(){\r\n\t\t\t\t\t\t\tif(window.jQuery){YRC_Loader(); window.clearInterval(yrctimer2324); }\r\n\t\t\t\t\t\t}, 250);}\r\n\t\t\t\t\t} else {if(YRC.EM)YRC.EM.trigger(\"yrc.newchannel\");}\r\n\t\t\t\t}());<\/script>\n<p style=\"text-align: center;\"><strong>You can also watch the video on YouTube <\/strong><a href=\"https:\/\/youtu.be\/sDyOLjixXT0\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>here<\/strong><\/a><\/p>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/sets_operations.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<h4 class=\"post_h4\">Add elements to set<\/h4>\n<p>One can add elements to set directly as <code>set(['one',1,'two',2])<\/code>\u00a0or there is a method called <code>add<\/code>\u00a0as <code>example.add('one')<\/code>\u00a0where example here is a set <code>example = set()<\/code><\/p>\n<p>Here in this example, I will add even, odd, prime, and composite numbers between 1 to 10 as different set elements.<\/p>\n<p>Initialize the sets,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># set of all numbers from 1 to 10\r\nnumbers = set()\r\n\r\n# set of all even numbers between 1 to 10\r\neven_numbers = set()\r\n\r\n# set of all odd numbers between 1 to 10\r\nodd_numbers = set()\r\n\r\n# set of all prime numbers between 1 to 10\r\nprime_numbers = set()\r\n\r\n# set of all composite numbers between 1 to 10\r\ncomposite_numbers = set()<\/pre>\n<p>Now I will add elements to these sets, here I can manually add elements to the set as explained before <code>numbers = set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])<\/code>\u00a0but I will do it programmatically here, in the previous tutorials I said how to find <a href=\"http:\/\/programminginpython.com\/python-program-to-find-whether-a-number-is-even-or-odd\/\" target=\"_blank\" rel=\"noopener noreferrer\">even<\/a>, <a href=\"http:\/\/programminginpython.com\/python-program-to-find-whether-a-number-is-even-or-odd\/\" target=\"_blank\" rel=\"noopener noreferrer\">odd<\/a>, <a href=\"http:\/\/programminginpython.com\/python-program-find-number-prime-composite\/\" target=\"_blank\" rel=\"noopener noreferrer\">prime<\/a>, and <a href=\"http:\/\/programminginpython.com\/python-program-find-number-prime-composite\/\" target=\"_blank\" rel=\"noopener noreferrer\">composite<\/a> numbers, so I will use that logic to add elements to the respected\u00a0set here.<\/p>\n<div class=\"code-embed-wrapper\">\n<div class=\"code-embed-infos\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">for i in range(1, 11):\r\n    numbers.add(i)\r\n    even_odd_sets(i)\r\n    prime_composite_sets(i)<\/pre>\n<\/div>\n<\/div>\n<p>functions for <code>even_odd_sets<\/code> and <code>prime_composite_sets<\/code><\/p>\n<div class=\"code-embed-wrapper\">\n<div class=\"code-embed-infos\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># function which finds even and odd numbers\r\ndef even_odd_sets(num):\r\n    if num % 2 == 0:\r\n        even_numbers.add(num)\r\n    else:\r\n        odd_numbers.add(num)\r\n\r\n\r\n# function which finds prime and composite numbers\r\ndef prime_composite_sets(num):\r\n    if num &gt; 1:\r\n        for j in range(2, num):\r\n            if (num % j) == 0:\r\n                composite_numbers.add(num)\r\n                break\r\n        else:\r\n            prime_numbers.add(num)<\/pre>\n<\/div>\n<\/div>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/sets_operations.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<h4 class=\"post_h4\">Length of a set<\/h4>\n<p>Python has an inbuilt function <code>len(set)<\/code> for finding the length of a set. So I will find the length of the above 5 sets which have different types of numbers in them.<\/p>\n<div class=\"code-embed-wrapper\">\n<div class=\"code-embed-infos\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># Length of the set\r\n\r\nprint(\"Length of set numbers:\",\r\n      len(numbers))\r\n\r\nprint(\"Length of set even numbers:\",\r\n      len(even_numbers))\r\n\r\nprint(\"Length of set odd numbers:\",\r\n      len(odd_numbers))\r\n\r\nprint(\"Length of set prime numbers:\",\r\n      len(prime_numbers))\r\n\r\nprint(\"Length of set composite numbers:\",\r\n      len(composite_numbers))<\/pre>\n<\/div>\n<\/div>\n<p>The above lines produce the following output,<\/p>\n<figure id=\"attachment_844\" aria-describedby=\"caption-attachment-844\" style=\"width: 384px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-844 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/05\/set_operations_1.png\" alt=\"Set Operations in Python\" width=\"384\" height=\"280\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 384px; --smush-placeholder-aspect-ratio: 384\/280;\" \/><figcaption id=\"caption-attachment-844\" class=\"wp-caption-text\">Set Operations in Python<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<h4 class=\"post_h4\">Intersection of sets<\/h4>\n<p>Intersection means the common elements in any given 2 sets, it is generally denoted as ? in mathematics, assume A and B are 2 sets, then consider the following diagram where <code>A ? B<\/code> are the common elements in both sets.<\/p>\n<figure id=\"attachment_844\" aria-describedby=\"caption-attachment-844\" style=\"width: 384px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-844 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/05\/set_operations_2.png\" alt=\"Set Operations in Python\" width=\"384\" height=\"280\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 384px; --smush-placeholder-aspect-ratio: 384\/280;\" \/><figcaption id=\"caption-attachment-844\" class=\"wp-caption-text\">Set Operations in Python<\/figcaption><\/figure>\n<p>So we can implement that intersection operation in Python by using the intersection method on one set with the other set. Ex: <code>A.intersection(B)<\/code>==&gt; A ? B<\/p>\n<p>I will apply this to the numbers example I am using for this tutorial.<\/p>\n<div class=\"code-embed-wrapper\">\n<div class=\"code-embed-infos\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># Intersection of sets\r\n\r\nprint(\"\\n\\nIntersection of numbers and even_numbers:\",\r\n      numbers.intersection(even_numbers))\r\n\r\nprint(\"Intersection of numbers and odd_numbers:\",\r\n      numbers.intersection(odd_numbers))\r\n\r\nprint(\"Intersection of numbers and prime_numbers:\",\r\n      numbers.intersection(prime_numbers))\r\n\r\nprint(\"Intersection of numbers and composite_numbers:\",\r\n      numbers.intersection(composite_numbers))\r\n\r\nprint(\"Intersection of prime numbers and composite_numbers:\",\r\n      prime_numbers.intersection(composite_numbers))<\/pre>\n<\/div>\n<\/div>\n<h4 class=\"post_h4\">Union of sets<\/h4>\n<p>Union of any 2 sets returns all the elements in both the sets, it is generally denoted as\u00a0? in mathematics,\u00a0assume A and B are 2 sets, then consider the following diagram where <code>A ? B<\/code> are\u00a0all the elements in both the sets.<\/p>\n<p>So I will implement this union operation in python by using a function called union(), Ex: <code>A.union(B)<\/code><\/p>\n<p>Now I will apply this to our numbers example.<\/p>\n<div class=\"code-embed-wrapper\">\n<div class=\"code-embed-infos\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># Union of sets\r\n\r\nprint(\"\\n\\nUnion of numbers and even_numbers:\",\r\n      numbers.union(even_numbers))\r\n\r\nprint(\"Union of numbers and odd_numbers:\",\r\n      numbers.union(odd_numbers))\r\n\r\nprint(\"Union of prime numbers and composite_numbers:\",\r\n      prime_numbers.union(composite_numbers))<\/pre>\n<\/div>\n<\/div>\n<h4 class=\"post_h4\">Difference between sets<\/h4>\n<p>The difference between the two sets is all the elements in the first set are not in the second.<\/p>\n<p>A \u2013 B is the Set of all elements in A but not in B<\/p>\n<p>B \u2013 A is the Set of all elements in B but not in A<\/p>\n<p>See the below Venn diagrams,<\/p>\n<p>A-B Venn diagram<\/p>\n<figure id=\"attachment_844\" aria-describedby=\"caption-attachment-844\" style=\"width: 384px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-844 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/set_operations_7-1.png\" alt=\"Set Operations in Python\" width=\"384\" height=\"280\" data-srcset=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/set_operations_7-1.png 384w, https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/set_operations_7-1.png 300w\" data-sizes=\"(max-width: 384px) 100vw, 384px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 384px; --smush-placeholder-aspect-ratio: 384\/280;\" \/><figcaption id=\"caption-attachment-844\" class=\"wp-caption-text\">Set Operations in Python<\/figcaption><\/figure>\n<p>B-A Venn diagram<\/p>\n<figure id=\"attachment_844\" aria-describedby=\"caption-attachment-844\" style=\"width: 384px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-844 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/05\/set_operations_6-1.png\" alt=\"Set Operations in Python\" width=\"384\" height=\"280\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 384px; --smush-placeholder-aspect-ratio: 384\/280;\" \/><figcaption id=\"caption-attachment-844\" class=\"wp-caption-text\">Set Operations in Python<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>This is implemented in Python by using a function called difference(), Ex: <code>A.difference(B)<\/code>\u00a0is the notation for A-B<\/p>\n<p>Now I will implement our numbers example.<\/p>\n<div class=\"code-embed-wrapper\">\n<div class=\"code-embed-infos\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># Difference of sets\r\n\r\nprint(\"\\n\\nDifference between numbers and prime_numbers:\",\r\n      numbers.difference(prime_numbers))\r\n\r\nprint(\"Difference between prime_numbers and numbers:\",\r\n      prime_numbers.difference(numbers))<\/pre>\n<\/div>\n<\/div>\n<p>If you observe the above output, the difference between <code>numbers<\/code> and <code>prime_numbers<\/code> gave all the elements in <code>numbers<\/code> set which are not in <code>prime_numbers<\/code> set and the difference <code>prime_numbers<\/code> and <code>numbers<\/code> returned an empty set because there are no elements in <code>prime_numbers<\/code>\u00a0which are not in numbers.<\/p>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/sets_operations.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<h4 class=\"post_h4\">Remove an element from a set<\/h4>\n<p>We can also remove a single element. There are two functions for doing it <code>remove()<\/code> and <code>discard()<\/code><\/p>\n<p>Ex: <code>set.remove('element')<\/code><\/p>\n<p>Ex: <code>set.discard('element')<\/code><\/p>\n<p>The difference between these two functions remove and discard is that, if the element we are deleting is not present, then the remove function throws an error whereas the discard function, just removes if the element is present or else does nothing.<\/p>\n<div class=\"code-embed-wrapper\">\n<div class=\"code-embed-infos\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">example = set(['test', 43, 'another', 120])\r\nprint(\"\\nExample Set:\", example)\r\n\r\n# Remove element in a set\r\nexample.remove(120)\r\nprint(example)\r\n\r\nexample.discard(43)\r\nprint(example)<\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_843\" aria-describedby=\"caption-attachment-843\" style=\"width: 759px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-843 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/set_operations_9.png\" alt=\"Set Operations in Python\" width=\"759\" height=\"227\" data-srcset=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/set_operations_9.png 759w, https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/set_operations_9.png 300w\" data-sizes=\"(max-width: 640px) 100vw, 640px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 759px; --smush-placeholder-aspect-ratio: 759\/227;\" \/><figcaption id=\"caption-attachment-843\" class=\"wp-caption-text\">Set Operations in Python<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<h4 class=\"post_h4\">Clear all the elements<\/h4>\n<p>And finally, we can clear all the elements in a set by using an inbuilt function in Python for sets called <code>clear()<\/code>.Simply call this method on any set and all the elements in it will be cleared and will become empty.<\/p>\n<div class=\"code-embed-wrapper\">\n<div class=\"code-embed-infos\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">example = set(['test', 43, 'another', 120])\r\nprint(\"\\nExample Set:\", example)\r\n\r\n# Clear the set\r\nexample.clear()\r\nprint(example)<\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_842\" aria-describedby=\"caption-attachment-842\" style=\"width: 780px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-842 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/set_operations_10.png\" alt=\"Set Operations in Python\" width=\"780\" height=\"231\" data-srcset=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/set_operations_10.png 780w, https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/set_operations_10.png 300w, https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/set_operations_10.png 768w\" data-sizes=\"(max-width: 640px) 100vw, 640px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 780px; --smush-placeholder-aspect-ratio: 780\/231;\" \/><figcaption id=\"caption-attachment-842\" class=\"wp-caption-text\">Set Operations in Python<\/figcaption><\/figure>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/sets_operations.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<p>That\u2019s it for this post guys, also feel free to check the programs on patterns <a href=\"https:\/\/www.programminginpython.com\/category\/patterns\/\">here<\/a>\u00a0or find some programs on algorithms\u00a0<a href=\"https:\/\/www.programminginpython.com\/category\/algorithms\/\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone, welcome back to programminginpython.com. Here in this post, I am going to discuss another data type in python called SET, which is an unordered collection of data and is iterable, and has no &hellip;<\/p>\n","protected":false},"author":1,"featured_media":336,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,13],"tags":[47,99,67,66],"class_list":["post-45","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structures","category-sets","tag-data-structures","tag-intersection","tag-sets","tag-union"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Set Operations in Python - Programming In Python<\/title>\n<meta name=\"description\" content=\"Set Operations in Python - Programming In Python\" \/>\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.programminginpython.com\/set-operations-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Set Operations in Python - Programming In Python\" \/>\n<meta property=\"og:description\" content=\"Set Operations in Python - Programming In Python\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.programminginpython.com\/set-operations-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Programming In Python\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/programminginpython\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-30T04:56:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-21T19:09:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Set-Operations-Python-Programming-In-Python1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"AVINASH NETHALA\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@python_pip\" \/>\n<meta name=\"twitter:site\" content=\"@python_pip\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"AVINASH NETHALA\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Set Operations in Python - Programming In Python","description":"Set Operations in Python - Programming In Python","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.programminginpython.com\/set-operations-python\/","og_locale":"en_US","og_type":"article","og_title":"Set Operations in Python - Programming In Python","og_description":"Set Operations in Python - Programming In Python","og_url":"https:\/\/www.programminginpython.com\/set-operations-python\/","og_site_name":"Programming In Python","article_publisher":"https:\/\/www.facebook.com\/programminginpython","article_published_time":"2023-03-30T04:56:53+00:00","article_modified_time":"2023-05-21T19:09:30+00:00","og_image":[{"width":1920,"height":400,"url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Set-Operations-Python-Programming-In-Python1.png","type":"image\/png"}],"author":"AVINASH NETHALA","twitter_card":"summary_large_image","twitter_creator":"@python_pip","twitter_site":"@python_pip","twitter_misc":{"Written by":"AVINASH NETHALA","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.programminginpython.com\/set-operations-python\/#article","isPartOf":{"@id":"https:\/\/www.programminginpython.com\/set-operations-python\/"},"author":{"name":"AVINASH NETHALA","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/9a3c14fe46d422ebf783ee61de1e788c"},"headline":"Set Operations in Python","datePublished":"2023-03-30T04:56:53+00:00","dateModified":"2023-05-21T19:09:30+00:00","mainEntityOfPage":{"@id":"https:\/\/www.programminginpython.com\/set-operations-python\/"},"wordCount":763,"commentCount":0,"publisher":{"@id":"https:\/\/www.programminginpython.com\/#organization"},"image":{"@id":"https:\/\/www.programminginpython.com\/set-operations-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Set-Operations-Python-Programming-In-Python1.png","keywords":["data structures","intersection","sets","union"],"articleSection":["Data Structures","Sets"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.programminginpython.com\/set-operations-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.programminginpython.com\/set-operations-python\/","url":"https:\/\/www.programminginpython.com\/set-operations-python\/","name":"Set Operations in Python - Programming In Python","isPartOf":{"@id":"https:\/\/www.programminginpython.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.programminginpython.com\/set-operations-python\/#primaryimage"},"image":{"@id":"https:\/\/www.programminginpython.com\/set-operations-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Set-Operations-Python-Programming-In-Python1.png","datePublished":"2023-03-30T04:56:53+00:00","dateModified":"2023-05-21T19:09:30+00:00","description":"Set Operations in Python - Programming In Python","breadcrumb":{"@id":"https:\/\/www.programminginpython.com\/set-operations-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.programminginpython.com\/set-operations-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/set-operations-python\/#primaryimage","url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Set-Operations-Python-Programming-In-Python1.png","contentUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Set-Operations-Python-Programming-In-Python1.png","width":1920,"height":400,"caption":"Set Operations in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.programminginpython.com\/set-operations-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.programminginpython.com\/"},{"@type":"ListItem","position":2,"name":"Set Operations in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.programminginpython.com\/#website","url":"https:\/\/www.programminginpython.com\/","name":"Programming In Python","description":"All About Python","publisher":{"@id":"https:\/\/www.programminginpython.com\/#organization"},"alternateName":"pip","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.programminginpython.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.programminginpython.com\/#organization","name":"Programming In Python","alternateName":"PIP","url":"https:\/\/www.programminginpython.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/04\/pip_logo_500_500.png","contentUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/04\/pip_logo_500_500.png","width":500,"height":500,"caption":"Programming In Python"},"image":{"@id":"https:\/\/www.programminginpython.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/programminginpython","https:\/\/x.com\/python_pip","https:\/\/www.youtube.com\/programminginpython","https:\/\/github.com\/avinashn\/programminginpython.com"]},{"@type":"Person","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/9a3c14fe46d422ebf783ee61de1e788c","name":"AVINASH NETHALA","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ed52e7670d7db94820c7430d324103ccdecb16d86611d5b29064aa9ce25a958b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ed52e7670d7db94820c7430d324103ccdecb16d86611d5b29064aa9ce25a958b?s=96&d=mm&r=g","caption":"AVINASH NETHALA"},"sameAs":["https:\/\/www.programminginpython.com\/"],"url":"https:\/\/www.programminginpython.com\/author\/avinash\/"}]}},"jetpack_featured_media_url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Set-Operations-Python-Programming-In-Python1.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":7,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"predecessor-version":[{"id":851,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/45\/revisions\/851"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/media\/336"}],"wp:attachment":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}