Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Print a List Without Commas in Python

#1
How to Print a List Without Commas in Python

<div>
<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;763059&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;}">
<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" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Problem Formulation</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Given a <a rel="noreferrer noopener" title="The Ultimate Guide to Python Lists" href="https://blog.finxter.com/python-lists/" target="_blank">Python list </a>of elements. </p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">If you print the list to the shell using <code>print([1, 2, 3])</code>, the output is enclosed in square brackets and separated by commas like so: </p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio"><code>[1, 2, 3]</code></p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">But you want the list without commas like so: </p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio"><code>[1 2 3]</code></p>
<pre class="EnlighterJSRAW wp-embed-aspect-16-9 wp-has-aspect-ratio" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print([1, 2, 3])
# Output: [1, 2, 3]
# Desired: [1 2 3]</pre>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio"><strong>How to print the list without separating commas in Python?</strong></p>
<p>Note that this is slightly different to those two problem variants—feel free to click there to learn more about those problem variants:</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/how-to-print-a-list-without-brackets-and-commas-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-print-a-list-without-brackets-and-commas-in-python/" target="_blank" rel="noreferrer noopener">How to Print a List Without Brackets and Commas in Python?</a></p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/how-to-print-a-list-without-brackets-in-python/" data-type="post" data-id="34638">How</a><a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-print-a-list-without-brackets-in-python/" data-type="post" data-id="34638" target="_blank"> </a><a href="https://blog.finxter.com/how-to-print-a-list-without-brackets-in-python/" data-type="post" data-id="34638">to Print a List Without Brackets in Python?</a></p>
</p>
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Method 1: Unpacking Multiple Values into Print Function</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">The <a title="What is the Asterisk / Star Operator (*) in Python?" rel="noreferrer noopener" href="https://blog.finxter.com/what-is-asterisk-in-python/" target="_blank">asterisk operator</a> <code>*</code> is used to unpack an <a rel="noreferrer noopener" title="Iterators, Iterables and Itertools" href="https://blog.finxter.com/iterators-iterables-and-itertools/" target="_blank">iterable </a>into the argument list of a given function. </p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background">You can unpack all list elements into the <code><a title="Python print()" rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" target="_blank">print()</a></code> function to print all values individually, separated by an empty space per default (that you can override using the <code>sep</code> argument). For example, the expression <code>print('[', *lst, ']')</code> prints the elements in <code>my_list</code>, empty space separated, with the enclosing square brackets and without the separating commas!</p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Here’s an example:</p>
<pre class="EnlighterJSRAW wp-embed-aspect-16-9 wp-has-aspect-ratio" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lst = [1, 2, 3]
print('[', *lst, ']')
# [ 1 2 3 ]</pre>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">You can learn about the ins and outs of the built-in <code><a href="https://blog.finxter.com/python-print/" target="_blank" rel="noreferrer noopener" title="Python print()">print()</a></code> function in the following video:</p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-print-a-list-without-commas-in-python/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FyXhEvg8Domk%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">To master the basics of unpacking, feel free to check out this video on the asterisk operator:</p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-print-a-list-without-commas-in-python/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FyXhEvg8Domk%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Method 2: String Replace Method</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background">A simple way to print a list without commas is to first <a href="https://blog.finxter.com/convert-list-to-string/" data-type="post" data-id="9927">convert </a><a rel="noreferrer noopener" href="https://blog.finxter.com/convert-list-to-string/" data-type="post" data-id="9927" target="_blank">t</a><a href="https://blog.finxter.com/convert-list-to-string/" data-type="post" data-id="9927">he list to a string</a> using the built-in <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-str-function/" data-type="post" data-id="23735" target="_blank">str()</a></code> function. Then modify the resulting string representation of the list by using the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-replace-2/" data-type="post" data-id="26083" target="_blank">string.replace()</a></code> method until you get the desired result.</p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Here’s an example:</p>
<pre class="EnlighterJSRAW wp-embed-aspect-16-9 wp-has-aspect-ratio" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="9" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_list = [1, 2, 3] # Convert List to String
s = str(my_list)
print(s)
# [1, 2, 3] # Replace Separating Commas
s = s.replace(',', '') # Print List Without Commas
print(s)
# [1 2 3]</pre>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">The last line of the code snippet shows that the commas are removed from the output.</p>
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Method 3: String Join With Generator Expression</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-pale-cyan-blue-background-color has-background">You can print a list without commas using the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-join/" data-type="post" data-id="26062" target="_blank">string.join()</a></code> method on any separator string such as <code>' '</code> or <code>'\t'</code>. Pass a <a rel="noreferrer noopener" title="How to Use Generator Expressions in Python Dictionaries" href="https://blog.finxter.com/how-to-use-generator-expressions-in-python-dictionaries/" target="_blank">generator expression</a> to convert each list element to a string using the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-str-function/" data-type="post" data-id="23735" target="_blank">str()</a></code> built-in function. </p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Specifically, the expression <code>print('[', ' '.join(str(x) for x in my_list), ']')</code> prints <code>my_list</code> to the shell without separating commas.</p>
<pre class="EnlighterJSRAW wp-embed-aspect-16-9 wp-has-aspect-ratio" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_list = [1, 2, 3]
print('[', ' '.join(str(x) for x in my_list), ']')
# Output: [ 1 2 3 ]
</pre>
<ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio">
<li>The <code><a href="https://blog.finxter.com/python-string-join/" title="Python String join()" target="_blank" rel="noreferrer noopener">string.join(iterable)</a></code> method concatenates the elements in the given iterable.</li>
<li>The <code><a href="https://blog.finxter.com/python-str-function/" title="Python str() Function" target="_blank" rel="noreferrer noopener">str(object)</a></code> built-in function converts a given object to its string representation.</li>
<li><a href="https://blog.finxter.com/list-comprehension/" target="_blank" rel="noreferrer noopener" title="https://blog.finxter.com/list-comprehension/">Generator expressions</a> or list comprehensions are concise <a href="https://pythononeliners.com/" target="_blank" rel="noreferrer noopener" title="https://pythononeliners.com/">one-liner </a>ways to create a new iterable based by reusing elements from another iterable.</li>
</ul>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">You can dive deeper into generators in the following video:</p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-print-a-list-without-commas-in-python/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FyXhEvg8Domk%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: Combining the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-join/" data-type="post" data-id="26062" target="_blank">join()</a></code> method with a <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-use-generator-expressions-in-python-dictionaries/" data-type="post" data-id="7502" target="_blank">generator expression</a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/daily-python-puzzle-string-concatenation/" data-type="post" data-id="93" target="_blank">string concatenation</a> is the recommended approach of choice if you want to convert a list to a string without commas instead of printing it.</p>
<p>Here’s an example:</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="">my_list = [1, 2, 3]
s = '[' + ' '.join(str(x) for x in my_list) + ']'
print(s)
# Output: [ 1 2 3 ]
</pre>
<h2>Method 4: Print NumPy Array</h2>
<p class="has-global-color-8-background-color has-background">Sometimes it is sufficient to use the NumPy default output that is without separating commas. For example, if you <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print-one-line-list/" data-type="post" data-id="35722" target="_blank">print a list</a> it yields <code>[1, 2, 3]</code>. And if you print an array it yields <code>[1 2 3]</code>. You can easily <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-convert-a-list-to-a-numpy-array/" data-type="post" data-id="9743" target="_blank">convert a list to a NumPy array </a>using the <code>np.array(lst)</code> constructor.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np my_list = [1, 2, 3]
print(np.array(my_list))
# Output: [1 2 3]
</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-install-numpy-in-python/" data-type="post" data-id="35920" target="_blank">How to Install NumPy?</a></p>
<h2>Where to Go From Here?</h2>
<p>Enough theory. Let’s get some practice!</p>
<p>Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation. </p>
<p>To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>You build high-value coding skills by working on practical coding projects!</strong></p>
<p>Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?</p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f680.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> If your answer is <strong><em>YES!</em></strong>, consider becoming a <a rel="noreferrer noopener" href="https://blog.finxter.com/become-python-freelancer-course/" data-type="page" data-id="2072" target="_blank">Python freelance developer</a>! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>If you just want to learn about the freelancing opportunity, feel free to watch my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and learn how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
<h2>Programmer Humor</h2>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2753.png" alt="❓" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <code><strong>Question:</strong> How did the programmer <strong><em>die</em></strong> in the shower? &#x2620;</p>
<p>&#x2757; <strong>Answer</strong>: They read the shampoo bottle instructions: <br /><em><strong>Lather. Rinse. Repeat.</strong></em></code></p>
</div>


https://www.sickgaming.net/blog/2022/10/...in-python/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016