{"id":111973,"date":"2020-04-24T09:02:04","date_gmt":"2020-04-24T09:02:04","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=7879"},"modified":"2020-04-24T09:02:04","modified_gmt":"2020-04-24T09:02:04","slug":"convert-tuple-to-list","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/04\/24\/convert-tuple-to-list\/","title":{"rendered":"Convert Tuple to List"},"content":{"rendered":"<p><strong>Problem<\/strong>: Given a Python tuple with <code>n<\/code> elements. How to convert it into a list with the same <code>n<\/code> elements?<\/p>\n<p><strong>Examples<\/strong>: <\/p>\n<ul>\n<li>Convert tuple <code>(1, 2, 3, 4, 5)<\/code> into list <code>[1, 2, 3, 4, 5]<\/code>.<\/li>\n<li>Convert tuple <code>('Alice', 'Bob', 'Ann')<\/code> into list <code>['Alice', 'Bob', 'Ann']<\/code>.<\/li>\n<li>Convert tuple <code>(1,)<\/code> into list <code>[1]<\/code>.<\/li>\n<\/ul>\n<p><strong>Note<\/strong> <strong>Tuple<\/strong>: Tuples are similar to lists&#8212;with the difference that you cannot change the tuple values (tuples are immutable) and you use parentheses rather than square brackets.<\/p>\n<p><strong>Solution<\/strong>: Use the built-in Python <code>list()<\/code> function to convert a list into a tuple. You don&#8217;t need to import any external library.<\/p>\n<p><strong>Code<\/strong>: The following code converts the three given tuples into lists.<\/p>\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=\"\">tuple_1 = (1, 2, 3, 4, 5)\nprint(list(tuple_1))\n# [1, 2, 3, 4, 5] tuple_2 = ('Alice', 'Bob', 'Ann')\nprint(list(tuple_2))\n# ['Alice', 'Bob', 'Ann'] tuple_3 = (1,)\nprint(list(tuple_3))\n# [1]\n<\/pre>\n<p><strong>Try It Yourself<\/strong>: With our interactive code shell, you can try it yourself. As a small exercise, try to convert the empty tuple <code>()<\/code> into a list and see what happens.<\/p>\n<p> <iframe loading=\"lazy\" height=\"700px\" width=\"100%\" src=\"https:\/\/repl.it\/@finxter\/tupletolist?lite=true\" scrolling=\"no\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" sandbox=\"allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals\"><\/iframe> <\/p>\n<p><strong>Explanation<\/strong>: You can see that converting a tuple with one element leads to a list with one element. The <code>list()<\/code> function is the easiest way to convert a tuple into a list. Note that the values in the tuple are not copied&#8212;only a new reference to the same element is created:<\/p>\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/list_to_tuple-1024x576.jpg\" alt=\"\" class=\"wp-image-7866\" width=\"768\" height=\"432\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/list_to_tuple-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/list_to_tuple-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/list_to_tuple-768x432.jpg 768w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n<p>The graphic also shows how to convert a tuple back to a list by using the <code>tuple()<\/code> function (that&#8217;s also a Python built-in function). Thus, calling <code>list(tuple(lst))<\/code> on a list <code>lst<\/code> will result in a new list with the same elements.<\/p>\n<p><strong>Related articles:<\/strong><\/p>\n<ul>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-copy\/\" target=\"_blank\">List copy<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List complete guide<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/convert-list-to-tuple\/\" target=\"_blank\">Convert list to tuple<\/a><\/li>\n<li><a href=\"https:\/\/blog.finxter.com\/list-to-dict-convert-a-list-into-a-dictionary-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Convert list to dict<\/a><\/li>\n<\/ul>\n<p>Try to execute this code with the interactive Python tutor:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/pythontutor.com\/iframe-embed.html#code=list_1%20%3D%20%5B'Alice',%20'Bob',%20'Ann'%5D%0Atuple_1%20%3D%20tuple%28list_1%29%0Alist_2%20%3D%20list%28tuple_1%29%0A%0Aprint%28list_1%29%0Aprint%28tuple_1%29%0Aprint%28list_2%29%0A&amp;codeDivHeight=400&amp;codeDivWidth=350&amp;cumulative=false&amp;curInstr=6&amp;heapPrimitives=nevernest&amp;origin=opt-frontend.js&amp;py=3&amp;rawInputLstJSON=%5B%5D&amp;textReferences=false\" width=\"800\" height=\"500\" frameborder=\"0\"> <\/iframe> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a Python tuple with n elements. How to convert it into a list with the same n elements? Examples: Convert tuple (1, 2, 3, 4, 5) into list [1, 2, 3, 4, 5]. Convert tuple (&#8216;Alice&#8217;, &#8216;Bob&#8217;, &#8216;Ann&#8217;) into list [&#8216;Alice&#8217;, &#8216;Bob&#8217;, &#8216;Ann&#8217;]. Convert tuple (1,) into list [1]. Note Tuple: Tuples are [&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-111973","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\/111973","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=111973"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/111973\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=111973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=111973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=111973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}