{"id":187803,"date":"2023-04-10T11:06:48","date_gmt":"2023-04-10T15:06:48","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=187803"},"modified":"2023-04-19T15:37:47","modified_gmt":"2023-04-19T19:37:47","slug":"all-about-python-sets","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/all-about-python-sets\/","title":{"rendered":"All about Python Sets"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-background-on-sets\"><strong>Background on sets<\/strong><\/h2>\n\n\n\n<p>A set in&nbsp;<a href=\"https:\/\/theautomatic.net\/category\/python\/\">Python<\/a>&nbsp;is an unordered collection of unique elements. Sets are&nbsp;<strong>mutable<\/strong>&nbsp;and&nbsp;<strong>iterable<\/strong>&nbsp;(more on these properties later). Sets are useful for when dealing with a unique collection of elements \u2013 e.g. finding the unique elements within a list to determine if there are any values which should be present. The operations built around sets are also handy when you need to perform mathematical set-like operations. For example, how would you figure out the common elements between two&nbsp;<a href=\"https:\/\/theautomatic.net\/tutorial-for-python-lists\/\">lists<\/a>? Or what elements are in one list, but not another? With sets, it\u2019s easy!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-create-a-set\"><strong>How to create a set<\/strong><\/h2>\n\n\n\n<p>We can define a set using curly braces, similar to how we define dictionaries.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nums = {10, 20, 30, 40, 50}<\/pre>\n\n\n\n<p>We can confirm that&nbsp;<strong>nums<\/strong>&nbsp;is indeed a set by using the&nbsp;<strong>type<\/strong>&nbsp;function:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">type(nums)<\/pre>\n\n\n\n<p>We can also convert a list to a set like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">some_list = [1, 2, 3, 4, 5]\n \nsome_set = set(some_list)<\/pre>\n\n\n\n<p>By wrapping our list inside&nbsp;<em>set<\/em>, we can coerce our list into a set. Also, since sets contain only unique elements (e.g. no duplicates), if we convert a list containing duplicate values to a set, the set will give us just the unique elements.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">dup_values = [3, 4, 4, 4, 3, 3, 3, 4, 4, 5]\n \nset(dup_values)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"552\" height=\"149\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-convert-list-to-set-the-automatic-net.jpg\" alt=\"\" class=\"wp-image-187814 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-convert-list-to-set-the-automatic-net.jpg 552w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-convert-list-to-set-the-automatic-net-300x81.jpg 300w\" data-sizes=\"(max-width: 552px) 100vw, 552px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 552px; aspect-ratio: 552\/149;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to test if two sets are equal<\/strong><\/h2>\n\n\n\n<p>We can test if two sets are equal using the normal \u201c==\u201d operator.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">other_nums = {30, 10, 50, 40, 20}\n \nother_nums == nums # returns True\n \nnums == {20, 30} # returns False<\/pre>\n\n\n\n<p>Notice \u2013 since sets are unordered, it doesn\u2019t matter that the set&nbsp;<em>other_nums<\/em>&nbsp;looks like its elements are in a different order. Set equality comparison just takes into account if the two sets share the exact same elements, regardless of order. And that brings us to the next point \u2013 there\u2019s no indexing with sets. This means there\u2019s no such thing as the \u201cfirst\u201d element or \u201csecond element\u201d, etc. in a set (unlike a list). Each set is a just a container of unique,&nbsp;<em>unordered<\/em>&nbsp;elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Combining sets<\/strong><\/h2>\n\n\n\n<p>To combine, or take the union of sets, we use the&nbsp;<strong>union<\/strong>&nbsp;method:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">others = {60, 70, 80}\n \ncombined = nums.union(others)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"417\" height=\"253\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-union-sets-the-automatic-net.jpg\" alt=\"\" class=\"wp-image-187815 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-union-sets-the-automatic-net.jpg 417w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-union-sets-the-automatic-net-300x182.jpg 300w\" data-sizes=\"(max-width: 417px) 100vw, 417px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 417px; aspect-ratio: 417\/253;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Adding elements to sets<\/strong><\/h2>\n\n\n\n<p>To add an element (or elements) to a set, we use the&nbsp;<strong>add<\/strong>&nbsp;method.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># add 60 to nums set\nnums.add(60)\n \n# add 70 to nums set\nnums.add(70)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"530\" height=\"211\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-add-element-to-a-set-the-automatic-net.jpg\" alt=\"\" class=\"wp-image-187817 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-add-element-to-a-set-the-automatic-net.jpg 530w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-add-element-to-a-set-the-automatic-net-300x119.jpg 300w\" data-sizes=\"(max-width: 530px) 100vw, 530px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 530px; aspect-ratio: 530\/211;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Getting the intersection of two sets<\/strong><\/h2>\n\n\n\n<p>To get the intersection, or what common elements exist between two sets, we use the&nbsp;<strong>intersection<\/strong>&nbsp;method.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nums.intersection(others)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"390\" height=\"79\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-set-intersection-the-automatic-net.jpg\" alt=\"\" class=\"wp-image-187819 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-set-intersection-the-automatic-net.jpg 390w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-set-intersection-the-automatic-net-300x61.jpg 300w\" data-sizes=\"(max-width: 390px) 100vw, 390px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 390px; aspect-ratio: 390\/79;\" \/><\/figure>\n\n\n\n<p>Now, what if we want to find the common elements between two lists, rather than sets? All we have to do is convert our lists to sets, and we can use the same&nbsp;<strong>intersection<\/strong>&nbsp;method.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">list1 = [4, 5, 5, 6, 7]\nlist2 = [5, 7, 8]\n \nset(list).intersection(set(list2)) # returns {5, 7}<\/pre>\n\n\n\n<p><strong>How to get the difference between two sets<\/strong><\/p>\n\n\n\n<p>We can get the difference between two sets (i.e. what elements exist in one set, but not the other) using the&nbsp;<strong>difference<\/strong>&nbsp;method.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nums.difference(others)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"377\" height=\"69\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-set-difference-the-automatic-net.jpg\" alt=\"\" class=\"wp-image-187821 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-set-difference-the-automatic-net.jpg 377w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-set-difference-the-automatic-net-300x55.jpg 300w\" data-sizes=\"(max-width: 377px) 100vw, 377px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 377px; aspect-ratio: 377\/69;\" \/><\/figure>\n\n\n\n<p>Just like getting finding the common elements between lists with the&nbsp;<strong>intersection<\/strong>&nbsp;method, we can find what elements belong in one list, but not another by converting the lists into sets and applying the&nbsp;<strong>difference<\/strong>&nbsp;method. For example, the below code will return {1, 2} because 1 and 2 are each present in list1, but not list2.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">list1 = [1, 2, 3, 4]\nlist2 = [3, 4, 5]\n \nset(list1).difference(set(list2)) # returns {1, 2}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to get the symmetric difference between two sets<\/strong><\/h2>\n\n\n\n<p>The symmetric difference between two sets, A and B, are the set of elements that exist only in A, or only in B. We can illustrate with our example sets.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nums.symmetric_difference(others)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"504\" height=\"79\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-symmetric-difference-between-sets-the-automatic-net.jpg\" alt=\"\" class=\"wp-image-187824 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-symmetric-difference-between-sets-the-automatic-net.jpg 504w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-symmetric-difference-between-sets-the-automatic-net-300x47.jpg 300w\" data-sizes=\"(max-width: 504px) 100vw, 504px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 504px; aspect-ratio: 504\/79;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Adding a set of elements to another set<\/strong><\/h2>\n\n\n\n<p><strong><\/strong><\/p>\n\n\n\n<p>Using the&nbsp;<strong>add<\/strong>&nbsp;method above, we can add one element at a time to a set. But what if we want to add a full set of other elements at once? We can do that using the&nbsp;<strong>update<\/strong>&nbsp;method. This method is&nbsp;<strong>inplace<\/strong>, which means we don\u2019t need to assign a variable to the new result. Instead, calling&nbsp;<strong>nums.update(SOME SET GOES HERE)<\/strong>&nbsp;will update the value of&nbsp;<strong>nums<\/strong>&nbsp;to include whatever elements are in the input set. We can see this in the example below by updating&nbsp;<strong>nums<\/strong>&nbsp;to include the elements in the&nbsp;<strong>letters<\/strong>&nbsp;set that we define.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">letters = {\"a\", \"b\", \"c\"}\n \nnums.update(letters)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"561\" height=\"186\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-update-set-the-automatic-net.jpg\" alt=\"\" class=\"wp-image-187827 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-update-set-the-automatic-net.jpg 561w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-update-set-the-automatic-net-300x99.jpg 300w\" data-sizes=\"(max-width: 561px) 100vw, 561px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 561px; aspect-ratio: 561\/186;\" \/><\/figure>\n\n\n\n<p>If we want to take out these new elements, we can just use the previously mentioned&nbsp;<strong>difference<\/strong>&nbsp;method.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nums = nums.difference(letters)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to tell if two sets contain no common elements (disjoint)<\/strong><\/h2>\n\n\n\n<p>Python has several Boolean methods available for sets. For instance, we can check if two sets are disjoint (i.e. they do not share any common elements) by using the&nbsp;<strong>isdisjoint<\/strong>&nbsp;method.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nums.isdisjoint(letters) # returns True<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"424\" height=\"235\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-test-if-two-sets-are-disjoint-the-automatic-net.jpg\" alt=\"\" class=\"wp-image-187829 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-test-if-two-sets-are-disjoint-the-automatic-net.jpg 424w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-test-if-two-sets-are-disjoint-the-automatic-net-300x166.jpg 300w\" data-sizes=\"(max-width: 424px) 100vw, 424px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 424px; aspect-ratio: 424\/235;\" \/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\"a\", \"d\"}.isdisjoint(letters) # returns False<\/pre>\n\n\n\n<p><strong>Checking if a set is a subset of another<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sample = {\"a\", \"c\"}\n \nsample.issubset(letters) # returns True\n \nsample.issubset(nums) # returns False<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"342\" height=\"205\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-check-if-a-set-is-a-subset-of-another-the-automatic-net.jpg\" alt=\"\" class=\"wp-image-187830 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-check-if-a-set-is-a-subset-of-another-the-automatic-net.jpg 342w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-check-if-a-set-is-a-subset-of-another-the-automatic-net-300x180.jpg 300w\" data-sizes=\"(max-width: 342px) 100vw, 342px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 342px; aspect-ratio: 342\/205;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Checking if a set is a super set of another<\/strong><\/h2>\n\n\n\n<p>Similarly, we can also test if a set is a super set of another (i.e. A is a super set of B if every element in B is also in A, which also means that B must be a subset of A). In this case we use the&nbsp;<strong>superset<\/strong>&nbsp;method.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sample = {\"a\", \"c\"}\n \nletters.issuperset(sample) # returns True\n \nletters.issuperset(nums) # returns False<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"359\" height=\"199\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-superset-the-automatic-net.jpg\" alt=\"\" class=\"wp-image-187831 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-superset-the-automatic-net.jpg 359w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-superset-the-automatic-net-300x166.jpg 300w\" data-sizes=\"(max-width: 359px) 100vw, 359px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 359px; aspect-ratio: 359\/199;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to remove a single element from a set<\/strong><\/h2>\n\n\n\n<p>Removing a single element from a set can be done a couple of ways. One way is using the&nbsp;<strong>discard<\/strong>&nbsp;method. This method will&nbsp;<em>not<\/em>&nbsp;raise an error if the element to be removed is not actually present in the set. The&nbsp;<strong>discard<\/strong>&nbsp;method is inplace.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mixture = {\"a\", \"b\", 1, 2, 3}\n \n# after running this line\n# mixture no longer contains \"a\"\nmixture.discard(\"a\")\n \n# nothing happens to mixture\n# since the string \"something\" is not present\n# in the set\nmixture.discard(\"something\")<\/pre>\n\n\n\n<p>A single element can also be removed from a set using the&nbsp;<strong>remove<\/strong>&nbsp;method. The difference here is that the&nbsp;<strong>remove<\/strong>&nbsp;method will raise an error if the element to be removed is not present in the set.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># remove \"b\" from set\nmixture.remove(\"b\")\n \n# try to remove \"something\"\n# from set\nmixture.remove(\"something\") # error occurs<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Sets are mutable<\/strong><\/h2>\n\n\n\n<p><strong><\/strong><\/p>\n\n\n\n<p><strong>Mutable<\/strong>&nbsp;means that an object can be changed after it is instantiated. Thus, if we create a set, we can change its contents without formally re-defining the set. We\u2019ve already seen examples of this above. For example, consider again adding an element to a set.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># add 60 to nums set\nnums.add(60)\n \n# add 70 to nums set\nnums.add(70)<\/pre>\n\n\n\n<p>Here, we didn\u2019t have to re-define&nbsp;<em>nums<\/em>. We\u2019re allowed to add an element to the set we\u2019ve already created because of the fact that sets are&nbsp;<strong>mutable<\/strong>. If sets were not mutable, we would not be able to do this. To learn more about mutability,&nbsp;<a href=\"https:\/\/medium.com\/@meghamohan\/mutable-and-immutable-side-of-python-c2145cf72747\">see here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Sets are iterable<\/strong><\/h2>\n\n\n\n<p><strong><\/strong><\/p>\n\n\n\n<p>Sets are also&nbsp;<strong>iterable<\/strong>, which means we can loop over the elements of any set.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">test = {4, 5, 6, 7, 8}\n \n# print each element in test\nfor num in test:\n    print(num)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Set comprehensions<\/strong><\/h2>\n\n\n\n<p>Python also supports&nbsp;<strong>set comprehensions<\/strong>, which are very similar to&nbsp;<a href=\"https:\/\/theautomatic.net\/tutorial-on-python-list-comprehensions\/\">list comprhensions<\/a>, except they use sets instead of lists for their base construct.&nbsp;<a href=\"https:\/\/theautomatic.net\/tutorial-on-python-list-comprehensions\/\">Check out the tutorial on list comprehensions, and you\u2019ll be able to apply the same concepts to set comprehensions.<\/a><\/p>\n\n\n\n<p>Set comprehensions allow you to create a set by looping through either another set or some other object. For example, below we\u2019re able to create a set where every element is two times the elements in the original set,&nbsp;<em>test<\/em>. This is done my looping through each element in&nbsp;<em>test<\/em>&nbsp;(calling each element here \u201cnum\u201d), and returning num * 2 i.e. the element times two.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">test = {4, 5, 6, 7, 8}\n \n# multiply every element in test by 2\n{num * 2 for num in test}<\/pre>\n\n\n\n<p>Just like list comprehensions, set comprehensions also support looping and filtering in one step. Below we loop through each element in&nbsp;<em>test<\/em>&nbsp;and output that element to a new set if the element is less than 6 (\u201cnum &lt; 6\u201d).<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># get the subset of test where each number is less than 6\n{num for num in test if num &lt; 6}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"517\" height=\"213\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-set-comprehensions-the-automatic-net.jpg\" alt=\"\" class=\"wp-image-187834 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-set-comprehensions-the-automatic-net.jpg 517w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-set-comprehensions-the-automatic-net-300x124.jpg 300w\" data-sizes=\"(max-width: 517px) 100vw, 517px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 517px; aspect-ratio: 517\/213;\" \/><\/figure>\n\n\n\n<p><em>Originally posted on <a href=\"https:\/\/theautomatic.net\/2019\/05\/29\/all-about-python-sets\/\">TheAutomatic.net<\/a> blog<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sets are useful for when dealing with a unique collection of elements \u2013 e.g. finding the unique elements within a list to determine if there are any values which should be present. <\/p>\n","protected":false},"author":388,"featured_media":188169,"comment_status":"closed","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,349,338,341],"tags":[806,595],"contributors-categories":[13695],"class_list":{"0":"post-187803","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-python-development","10":"category-ibkr-quant-news","11":"category-quant-development","12":"tag-data-science","13":"tag-python","14":"contributors-categories-theautomatic-net"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>All about Python Sets | IBKR Quant<\/title>\n<meta name=\"description\" content=\"Sets are useful for when dealing with a unique collection of elements \u2013 e.g. finding the unique elements within a list to determine if there are any...\" \/>\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.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/187803\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"All about Python Sets | IBKR Campus US\" \/>\n<meta property=\"og:description\" content=\"Sets are useful for when dealing with a unique collection of elements \u2013 e.g. finding the unique elements within a list to determine if there are any values which should be present.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/all-about-python-sets\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-10T15:06:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-19T19:37:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/04\/python-gear-red-green-white.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Andrew Treadway\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrew Treadway\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/all-about-python-sets\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/all-about-python-sets\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Andrew Treadway\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d4018570a16fb867f1c08412fc9c64bc\"\n\t            },\n\t            \"headline\": \"All about Python Sets\",\n\t            \"datePublished\": \"2023-04-10T15:06:48+00:00\",\n\t            \"dateModified\": \"2023-04-19T19:37:47+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/all-about-python-sets\\\/\"\n\t            },\n\t            \"wordCount\": 1193,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/all-about-python-sets\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/04\\\/python-gear-red-green-white.jpg\",\n\t            \"keywords\": [\n\t                \"Data Science\",\n\t                \"Python\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\",\n\t                \"Quant Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/all-about-python-sets\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/all-about-python-sets\\\/\",\n\t            \"name\": \"All about Python Sets | IBKR Campus US\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/all-about-python-sets\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/all-about-python-sets\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/04\\\/python-gear-red-green-white.jpg\",\n\t            \"datePublished\": \"2023-04-10T15:06:48+00:00\",\n\t            \"dateModified\": \"2023-04-19T19:37:47+00:00\",\n\t            \"description\": \"Sets are useful for when dealing with a unique collection of elements \u2013 e.g. finding the unique elements within a list to determine if there are any values which should be present.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/all-about-python-sets\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/all-about-python-sets\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/04\\\/python-gear-red-green-white.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/04\\\/python-gear-red-green-white.jpg\",\n\t            \"width\": 1000,\n\t            \"height\": 563,\n\t            \"caption\": \"Python\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d4018570a16fb867f1c08412fc9c64bc\",\n\t            \"name\": \"Andrew Treadway\",\n\t            \"description\": \"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \\\/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\\\/\\\/www.linkedin.com\\\/in\\\/andrew-treadway-a3b19b103\\\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.\",\n\t            \"sameAs\": [\n\t                \"https:\\\/\\\/theautomatic.net\\\/about-me\\\/\"\n\t            ],\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/andrewtreadway\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"All about Python Sets | IBKR Quant","description":"Sets are useful for when dealing with a unique collection of elements \u2013 e.g. finding the unique elements within a list to determine if there are any...","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.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/187803\/","og_locale":"en_US","og_type":"article","og_title":"All about Python Sets | IBKR Campus US","og_description":"Sets are useful for when dealing with a unique collection of elements \u2013 e.g. finding the unique elements within a list to determine if there are any values which should be present.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/all-about-python-sets\/","og_site_name":"IBKR Campus US","article_published_time":"2023-04-10T15:06:48+00:00","article_modified_time":"2023-04-19T19:37:47+00:00","og_image":[{"width":1000,"height":563,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/04\/python-gear-red-green-white.jpg","type":"image\/jpeg"}],"author":"Andrew Treadway","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Andrew Treadway","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/all-about-python-sets\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/all-about-python-sets\/"},"author":{"name":"Andrew Treadway","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d4018570a16fb867f1c08412fc9c64bc"},"headline":"All about Python Sets","datePublished":"2023-04-10T15:06:48+00:00","dateModified":"2023-04-19T19:37:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/all-about-python-sets\/"},"wordCount":1193,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/all-about-python-sets\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/04\/python-gear-red-green-white.jpg","keywords":["Data Science","Python"],"articleSection":["Data Science","Programming Languages","Python Development","Quant","Quant Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/all-about-python-sets\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/all-about-python-sets\/","name":"All about Python Sets | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/all-about-python-sets\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/all-about-python-sets\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/04\/python-gear-red-green-white.jpg","datePublished":"2023-04-10T15:06:48+00:00","dateModified":"2023-04-19T19:37:47+00:00","description":"Sets are useful for when dealing with a unique collection of elements \u2013 e.g. finding the unique elements within a list to determine if there are any values which should be present.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/all-about-python-sets\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/all-about-python-sets\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/04\/python-gear-red-green-white.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/04\/python-gear-red-green-white.jpg","width":1000,"height":563,"caption":"Python"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d4018570a16fb867f1c08412fc9c64bc","name":"Andrew Treadway","description":"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\/\/www.linkedin.com\/in\/andrew-treadway-a3b19b103\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.","sameAs":["https:\/\/theautomatic.net\/about-me\/"],"url":"https:\/\/www.interactivebrokers.com\/campus\/author\/andrewtreadway\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/04\/python-gear-red-green-white.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/187803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/388"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=187803"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/187803\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/188169"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=187803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=187803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=187803"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=187803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}