[Tut] 5 Pythonic Ways to Print a List without Brackets - 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] 5 Pythonic Ways to Print a List without Brackets (/thread-99456.html) |
[Tut] 5 Pythonic Ways to Print a List without Brackets - xSicKxBot - 05-25-2022 5 Pythonic Ways to Print a List without Brackets <div><div class="kk-star-ratings kksr-valign-top kksr-align-left " data-payload="{"align":"left","id":"375124","slug":"default","valign":"top","reference":"auto","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})"}"> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend"> 5/5 – (1 vote) </div> </div> <h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Problem Formulation and Solution Overview</h2> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">In this article, you’ll learn how to print the contents of a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> without surrounding brackets in Python.</p> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">To make it more fun, we have the following running scenario:</p> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio"><em>You are a student and need to memorize the first 10 elements in the <a rel="noreferrer noopener" href="https://byjus.com/periodic-table/" data-type="URL" data-id="https://byjus.com/periodic-table/" target="_blank">Periodic Table</a>. This data is currently saved in a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> format. However, you would prefer it to display without brackets to remove any distractions. </em></p> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><em><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we write Python code to print a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> without brackets?</em></p> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this task by one of the following options:</p> <ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio"> <li><strong>Method 1</strong>: Use <a href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank" rel="noreferrer noopener"><code>print()</code></a> and <a href="https://blog.finxter.com/python-string-join/" data-type="URL" data-id="https://blog.finxter.com/python-string-join/"><code>join()</code></a></li> <li><strong>Method 2</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print(*, sep)</code></a></li> <li><strong>Method 3</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="URL" data-id="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank"><code>slicing</code></a></li> <li><strong>Method 4</strong>: Use <a href="https://blog.finxter.com/python-string-join/" data-type="URL" data-id="https://blog.finxter.com/python-string-join/"><code>join()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-map/" data-type="URL" data-id="https://blog.finxter.com/python-map/" target="_blank"><code>map()</code></a></li> <li><strong>Bonus</strong>: Strip quote characters from Method 3.</li> </ul> <hr class="wp-block-separator"/> <h2>Method 1: Use print() and join()</h2> <p class="has-global-color-8-background-color has-background">This method uses <a href="https://blog.finxter.com/python-string-join/" data-type="URL" data-id="https://blog.finxter.com/python-string-join/"><code>join()</code></a> to access each element of the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> passed. Then <a href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/"><code>print()</code></a> lets <a href="https://blog.finxter.com/python-string-join/" data-type="URL" data-id="https://blog.finxter.com/python-string-join/"><code>join()</code></a> know which separator to concatenate (append) to each element. The result is a <em>String</em>. </p> <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="">periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(' / '.join(periodic_els))</pre> <p>This code declares a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> of the first 10 element names of the <a rel="noreferrer noopener" href="https://byjus.com/periodic-table/" data-type="URL" data-id="https://byjus.com/periodic-table/" target="_blank">Periodic Table</a> and saves them to <code>periodic_els</code>.</p> <p>Next, <a href="https://blog.finxter.com/python-string-join/" data-type="URL" data-id="https://blog.finxter.com/python-string-join/"><code>join()</code></a> passes <code>periodic_els</code> as an argument and accesses each element, adding the appropriate separator character(s) as indicated in the <a href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/"><code>print</code></a> statement (<code>' / '</code>).</p> <p>Finally, the output is sent to the terminal as a <em>String data type</em>.</p> <figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"> <div class="wp-block-embed__wrapper"> <iframe loading="lazy" title="Python String Methods [Ultimate Guide]" width="780" height="439" src="https://www.youtube.com/embed/-UkcLQxzPA4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </div> </figure> <p><strong>Output</strong></p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td>H / He / Li / Be / B / C / N / O / F / Ne</td> </tr> </tbody> </table> </figure> <p>If we modified the <a href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/"><code>print</code></a> statement to have a comma (<code>','</code>) as a separator, the output would be as follows:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">elements = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(','.join(elements))</pre> <p><strong>Output</strong></p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td> H,He,Li,Be,B,C,N,O,F,Ne</td> </tr> </tbody> </table> </figure> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: We recommend trying different separating characters, including a space (<code>' '</code>) and an empty space (<code>''</code>) to see the output.</p> <div class="wp-block-image is-style-default"> <figure class="aligncenter size-full is-resized"><a href="https://academy.finxter.com/university/the-ultimate-guide-to-python-string-methods/" target="_blank" rel="noopener"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-235.png" alt="" class="wp-image-377839" width="368" height="637" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-235.png 368w, https://blog.finxter.com/wp-content/uploads/2022/05/image-235-173x300.png 173w" sizes="(max-width: 368px) 100vw, 368px" /></a></figure> </div> <hr class="wp-block-separator"/> <h2>Method 2: Use print(*, sep)</h2> <p class="has-global-color-8-background-color has-background">This method uses <a href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/"><code>print()</code></a> passing <code>*periodic_els</code>, and a separator character (<code>' '</code>) as arguments to the same.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(*periodic_els, sep=' ')</pre> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: Placing an asterisk (<code>*</code>) before the variable lets Python know to unpack/extract the variable, in this case, a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a>.</p> <p>This code declares a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> of the first 10 elements in the <a rel="noreferrer noopener" href="https://byjus.com/periodic-table/" data-type="URL" data-id="https://byjus.com/periodic-table/" target="_blank">Periodic Table</a> and saves them to <code>periodic_els</code>.</p> <p>Inside the <a href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/"><code>print()</code></a> statement <code>*periodic_els</code> is passed as the first argument. This tells Python to unpack the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> as indicated above. </p> <p>Next, the <code>sep</code> argument is passed to <a href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/"><code>print()</code></a>. This will concatenate (append) character(s) between each element, in this case, a space (<code>' '</code>).</p> <p>Finally, the output is sent to the terminal as a <em>String data type</em>.</p> <figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"> <div class="wp-block-embed__wrapper"> <iframe loading="lazy" title="Python Print Function [And Its SECRET Separator & End Arguments]" width="780" height="439" src="https://www.youtube.com/embed/yXhEvg8Domk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </div> </figure> <p><strong>Output</strong></p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td> H He Li Be B C N O F Ne</td> </tr> </tbody> </table> </figure> <hr class="wp-block-separator"/> <h2>Method 3: Use Slicing</h2> <p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="URL" data-id="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank"><code>slicing</code></a> to remove the brackets. However, the single quote characters surrounding each element will remain.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(str(periodic_els)[1:-1])</pre> <p>This code declares a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> of the first 10 elements in the <a rel="noreferrer noopener" href="https://byjus.com/periodic-table/" data-type="URL" data-id="https://byjus.com/periodic-table/" target="_blank">Periodic Table</a> and saves them to <code>periodic_els</code>.</p> <p>Next, the <a href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/"><code>print()</code></a> statement converts the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> to a string and then uses slicing to extract the output as indicated by <code>[1:-1]</code>.</p> <p>Finally, the output is sent to the terminal as a <em>String data type</em>.</p> <figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"> <div class="wp-block-embed__wrapper"> <iframe loading="lazy" title="The Ultimate Guide to Slicing in Python" width="780" height="439" src="https://www.youtube.com/embed/D2ZueuWXST8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </div> </figure> <p><strong>Output</strong></p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td> ‘H’, ‘He’, ‘Li’, ‘Be’, ‘B’, ‘C’, ‘N’, ‘O’, ‘F’, ‘Ne’</td> </tr> </tbody> </table> </figure> <hr class="wp-block-separator"/> <h2>Method 4: Use join() and map()</h2> <p class="has-global-color-8-background-color has-background">If <code>periodic_els</code> contained integers instead of strings, it would need to be converted to a <em>String</em> data type first. Then, <a href="https://blog.finxter.com/python-string-join/" data-type="URL" data-id="https://blog.finxter.com/python-string-join/"><code>join()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-map/" data-type="URL" data-id="https://blog.finxter.com/python-map/" target="_blank"><code>map()</code></a> are used to output the contents without brackets.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">periodic_els = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(', '.join(map(str, periodic_els)))</pre> <p>This code declares a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> of the first 10 elements in the <a rel="noreferrer noopener" href="https://byjus.com/periodic-table/" data-type="URL" data-id="https://byjus.com/periodic-table/" target="_blank">Periodic Table</a> and saves them to <code>periodic_els</code>.</p> <p>Next, <code>periodic_els</code> is converted to a <em>String</em>, and the iterable <a rel="noreferrer noopener" href="https://blog.finxter.com/python-map/" data-type="URL" data-id="https://blog.finxter.com/python-map/" target="_blank"><code>map()</code></a> object is accessed. Then, each element is evaluated, and a separator character<br />(‘, ‘) is placed between each element and concatenated. </p> <p>Finally, the output is sent to the terminal as a <em>String data type</em>.</p> <figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"> <div class="wp-block-embed__wrapper"> <iframe loading="lazy" title="Mastering the Python Map Function [+Video]" width="780" height="439" src="https://www.youtube.com/embed/tqph6mWC3m8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </div> </figure> <p><strong>Output</strong></p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td> 1, 2, 3, 4, 5, 6, 7, 8, 9,10</td> </tr> </tbody> </table> </figure> <hr class="wp-block-separator"/> <h2>Bonus: Strip quote characters from Method 3</h2> <p class="has-global-color-8-background-color has-background">This section expands on <strong>Method 3,</strong> where the resultant output contained quote characters (<code>'</code>) surrounding each element. This can easily be removed by running the following code.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(str(periodic_els)[1:-1].replace("'", ""))</pre> <p>The only difference between this code from <strong>Method 3</strong> and the code above is that <code><a href="https://blog.finxter.com/python-string-replace-2/" data-type="URL" data-id="https://blog.finxter.com/python-string-replace-2/" target="_blank" rel="noreferrer noopener">replace</a>("'", "")</code> is appended to the end.</p> <figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"> <div class="wp-block-embed__wrapper"> <iframe loading="lazy" title="Python String Replace | A Helpful Guide" width="780" height="439" src="https://www.youtube.com/embed/K9-_WSh9Qkk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </div> </figure> <p><strong>Output</strong></p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td> H, He, Li, Be, B, C, N, O, F, Ne</td> </tr> </tbody> </table> </figure> <hr class="wp-block-separator"/> <h2>Summary</h2> <p>These four (4) methods of how to print a List without Brackets should give you enough information to select the best one for your coding requirements.</p> <p>Good Luck & Happy Coding!</p> <hr class="wp-block-separator"/> </div> https://www.sickgaming.net/blog/2022/05/19/5-pythonic-ways-to-print-a-list-without-brackets/ |