Sick Gaming
[Tut] Convert Tuple to List - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] Convert Tuple to List (/thread-94722.html)



[Tut] Convert Tuple to List - xSicKxBot - 04-25-2020

Convert Tuple to List

<div><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>
<p><strong>Examples</strong>: </p>
<ul>
<li>Convert tuple <code>(1, 2, 3, 4, 5)</code> into list <code>[1, 2, 3, 4, 5]</code>.</li>
<li>Convert tuple <code>('Alice', 'Bob', 'Ann')</code> into list <code>['Alice', 'Bob', 'Ann']</code>.</li>
<li>Convert tuple <code>(1,)</code> into list <code>[1]</code>.</li>
</ul>
<p><strong>Note</strong> <strong>Tuple</strong>: Tuples are similar to lists—with the difference that you cannot change the tuple values (tuples are immutable) and you use parentheses rather than square brackets.</p>
<p><strong>Solution</strong>: Use the built-in Python <code>list()</code> function to convert a list into a tuple. You don’t need to import any external library.</p>
<p><strong>Code</strong>: The following code converts the three given tuples into lists.</p>
<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)
print(list(tuple_1))
# [1, 2, 3, 4, 5] tuple_2 = ('Alice', 'Bob', 'Ann')
print(list(tuple_2))
# ['Alice', 'Bob', 'Ann'] tuple_3 = (1,)
print(list(tuple_3))
# [1]
</pre>
<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>
<p> <iframe 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>
<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—only a new reference to the same element is created:</p>
<figure class="wp-block-image size-large is-resized"><img 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="(max-width: 768px) 100vw, 768px" /></figure>
<p>The graphic also shows how to convert a tuple back to a list by using the <code>tuple()</code> function (that’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>
<p><strong>Related articles:</strong></p>
<ul>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-copy/" target="_blank">List copy</a></li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">List complete guide</a></li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/convert-list-to-tuple/" target="_blank">Convert list to tuple</a></li>
<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>
</ul>
<p>Try to execute this code with the interactive Python tutor:</p>
<p> <iframe 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>
</div>


https://www.sickgaming.net/blog/2020/04/24/convert-tuple-to-list/