{"id":128222,"date":"2022-09-18T17:00:33","date_gmt":"2022-09-18T17:00:33","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=689757"},"modified":"2022-09-18T17:00:33","modified_gmt":"2022-09-18T17:00:33","slug":"python-set-to-tuple-tuple-to-set-3-easy-ways","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/18\/python-set-to-tuple-tuple-to-set-3-easy-ways\/","title":{"rendered":"Python Set to Tuple | Tuple to Set | 3 Easy Ways"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;689757&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Problem Formulation<\/strong>: Given a Python set. How to convert it to a tuple? And how to convert the tuple back to a set?<\/p>\n<p>There are three main ways:<\/p>\n<ul>\n<li><strong>Method 1<\/strong>: To convert a Python <strong>set to a tuple<\/strong>, use the <code>tuple(my_set)<\/code> function. <\/li>\n<li><strong>Method 2<\/strong>: To convert a Python <strong>tuple to a set<\/strong>, use the <code>set(my_tuple)<\/code> function. <\/li>\n<li><strong>Method 3<\/strong>: To convert a Python tuple of mutable elements to a set, use the expression <code>set(tuple(x) for x in my_tuple)<\/code> to avoid a <code>TypeError<\/code>.<\/li>\n<\/ul>\n<p>I&#8217;ll also give you a <strong>bonus method 4 <\/strong>that shows you what to do to retain the <strong>ordering information<\/strong> when converting a tuple to a set&#8212;so keep reading! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Method 1: Convert Set to Tuple with tuple()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">To convert a <a href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"post\" data-id=\"1908\" target=\"_blank\" rel=\"noreferrer noopener\">set<\/a> to a tuple, pass the set into the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-tuple\/\" data-type=\"post\" data-id=\"21575\" target=\"_blank\">tuple()<\/a><\/code> function. This is a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pythons-top-29-built-in-functions-with-examples\/\" data-type=\"post\" data-id=\"25777\" target=\"_blank\">built-in<\/a> Python function, so you don&#8217;t need to import or <a href=\"https:\/\/blog.finxter.com\/how-to-install-xxx-in-python\/\" data-type=\"post\" data-id=\"653128\" target=\"_blank\" rel=\"noreferrer noopener\">install<\/a> any library to use it. The return value is a new <a href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"post\" data-id=\"12043\" target=\"_blank\" rel=\"noreferrer noopener\">tuple<\/a> from the values in the set. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> Here&#8217;s an example where you convert the set <code>{1, 2, 3}<\/code> to a tuple <code>(1, 2, 3)<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">my_set = {1, 2, 3}\nmy_tuple = tuple(my_set)\nprint(my_tuple)\n# (1, 2, 3)<\/pre>\n<p>By the way, here&#8217;s an explainer video on this function:<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/python-set-to-tuple-tuple-to-set\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FViJ1E-52hq4%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<h2>Method 2: Convert Tuple to Set with set()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">To convert a tuple to a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"post\" data-id=\"1908\" target=\"_blank\">set<\/a>, pass the tuple into the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-set-function\/\" data-type=\"post\" data-id=\"23052\" target=\"_blank\">set()<\/a><\/code> function. This is a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pythons-top-29-built-in-functions-with-examples\/\" data-type=\"post\" data-id=\"25777\" target=\"_blank\">built-in<\/a> Python function, so you don&#8217;t need to import or <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-install-xxx-in-python\/\" data-type=\"post\" data-id=\"653128\" target=\"_blank\">install<\/a> any library. The return value is a new set from the values in the tuple. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> Here&#8217;s an example where you convert the tuple <code>(1, 2, 3)<\/code> to a set <code>{1, 2, 3}<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">my_tuple = (1, 2, 3)\nmy_set = set(my_tuple)\nprint(my_set)\n# {1, 2, 3}<\/pre>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/python-set-to-tuple-tuple-to-set\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FfZJsKQPlzRg%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<p><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/26a1.png\" alt=\"\u26a1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Problem<\/strong>: However, the conversion process from a tuple to set doesn&#8217;t always work because if you try to convert a tuple of mutable values, Python will raise the <code>TypeError: unhashable type<\/code>!<\/p>\n<p>Read on to learn more about this problem&#8212;and how to resolve it easily: <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Method 3: Convert Tuple to Set with Set Comprehension<\/h2>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> A <strong>Python set<\/strong> is an <strong>unordered collection of unique immutable elements<\/strong>. Each element must define explicitly or implicitly the <code><a href=\"https:\/\/blog.finxter.com\/python-__hash__-magic-method\/\" data-type=\"post\" data-id=\"38118\">__hash__()<\/a><\/code> dunder method, i.e., must be <a href=\"https:\/\/blog.finxter.com\/how-to-fix-typeerror-unhashable-type-list\/\" data-type=\"post\" data-id=\"465745\" target=\"_blank\" rel=\"noreferrer noopener\">hashable<\/a>.<\/p>\n<p>If you attempt to convert a tuple of <a href=\"https:\/\/blog.finxter.com\/mutable-vs-immutable-objects-in-python\/\" data-type=\"post\" data-id=\"204090\" target=\"_blank\" rel=\"noreferrer noopener\">mutable<\/a> elements (e.g., <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"post\" data-id=\"7332\" target=\"_blank\" rel=\"noreferrer noopener\">lists<\/a>) to a set, Python will raise an error such as the <code>TypeError: unhashable type: 'list'<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"3\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">my_tuple = ([1, 2], [3, 4], [5, 6])\nmy_set = set(my_tuple)\n# TypeError: unhashable type: 'list'<\/pre>\n<p class=\"has-global-color-8-background-color has-background\">In this case, you can use <a href=\"https:\/\/blog.finxter.com\/brackets-a-simple-introduction-to-set-comprehension-in-python\/\" data-type=\"post\" data-id=\"1153\">set co<\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/brackets-a-simple-introduction-to-set-comprehension-in-python\/\" data-type=\"post\" data-id=\"1153\" target=\"_blank\">m<\/a><a href=\"https:\/\/blog.finxter.com\/brackets-a-simple-introduction-to-set-comprehension-in-python\/\" data-type=\"post\" data-id=\"1153\">prehension<\/a> to convert each inner tuple element to an immutable type. For example, the expression <code>set(tuple(x) for x in my_tuple)<\/code> converts each inner <a href=\"https:\/\/blog.finxter.com\/convert-list-to-tuple\/\" data-type=\"post\" data-id=\"7862\" target=\"_blank\" rel=\"noreferrer noopener\">list to a tuple<\/a>. The result is a set of immutable tuples.<\/p>\n<p>Here&#8217;s the solution to this problem in a minimal code example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">my_tuple = ([1, 2], [3, 4], [5, 6])\nmy_set = set(tuple(x) for x in my_tuple)\nprint(my_set)\n# {(1, 2), (3, 4), (5, 6)}\n<\/pre>\n<p>The final &#8220;bonus&#8221; section introduces another elegant way to retain the ordering information in a set:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"498\" height=\"498\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/09\/BonusbaadshahGIF.gif\" alt=\"\" class=\"wp-image-689976\"\/><\/figure>\n<\/div>\n<h2>Bonus Method 4: Enumerate Elements<\/h2>\n<p>Sometimes, you want to associate each set or tuple element with a specific numerical &#8220;index&#8221; value, i.e., a unique integer identifier. The <code><a href=\"https:\/\/blog.finxter.com\/python-enumerate\/\" data-type=\"post\" data-id=\"20466\" target=\"_blank\" rel=\"noreferrer noopener\">enumerate()<\/a><\/code> method to the rescue!<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/python-set-to-tuple-tuple-to-set\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F6SifUNXKWNA%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<ul>\n<li>Use <code>tuple(enumerate(my_set))<\/code> to convert a set to an enumerated tuple. <\/li>\n<li>Use <code>set(enumerate(my_tuple))<\/code> to convert a tuple to an enumerated set. <\/li>\n<\/ul>\n<p>The result is the respective container data structure with (identifier, value) tuples:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"3, 7\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">my_set = {'Alice', 'Bob', 'Carl'} my_tuple = tuple(enumerate(my_set))\nprint(my_tuple)\n# ((0, 'Carl'), (1, 'Bob'), (2, 'Alice')) my_set = set(enumerate(('Alice', 'Bob', 'Carl')))\nprint(my_set)\n# {(2, 'Carl'), (0, 'Alice'), (1, 'Bob')}\n<\/pre>\n<p>Especially in the case where you convert a tuple to a set, this makes a lot of sense because you can retain the information on the ordering of elements that would be otherwise lost after converting to a set.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<p>Thanks for reading the whole article, my friend! You can join us <a href=\"https:\/\/blog.finxter.com\/subscribe\/\" data-type=\"page\" data-id=\"1414\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> (we have cheat sheets too):<\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation: Given a Python set. How to convert it to a tuple? And how to convert the tuple back to a set? There are three main ways: Method 1: To convert a Python set to a tuple, use the tuple(my_set) function. Method 2: To convert a Python tuple to a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-128222","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=128222"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128222\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}