Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Print Dictionary Values Without “dict_values”

#1
Python Print Dictionary Values Without “dict_values”

<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;772646&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>Problem Formulation and Solution Overview</h2>
<p class="has-global-color-8-background-color has-background">If you print all values from a dictionary in Python using <code>print(dict.values())</code>, Python returns a <code>dict_values</code> object, a view of the dictionary values. The representation prints the keys enclosed in a weird <code>dict_values(...)</code>, for example: <code>dict_values([1, 2, 3])</code>.</p>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4,5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(my_dict.values())
# dict_values(['Carl', 42, 100000])</pre>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="701" height="182" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-57.png" alt="" class="wp-image-772935" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-57.png 701w, https://blog.finxter.com/wp-content/uplo...300x78.png 300w" sizes="(max-width: 701px) 100vw, 701px" /></figure>
</div>
<p>There are multiple ways to change the string representation of the values, so that the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank">print()</a></code> output doesn’t yield the strange <code>dict_values</code> view object. </p>
</p>
<h2>Method 1: Convert to List</h2>
<p class="has-global-color-8-background-color has-background">An easy way to obtain a pretty output when printing the dictionary values without <code>dict_values(...)</code> representation is to convert the <code>dict_value</code> object to a list using the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-list/" data-type="post" data-id="21502" target="_blank">list() </a></code>built-in function. For instance, <code>print(list(my_dict.value()))</code> prints the dictionary values as a simple <a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank" rel="noreferrer noopener">list</a>.</p>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4-5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(list(my_dict.values()))
# ['Carl', 42, 100000]</pre>
<p>So far, so simple. Read on to learn or recap some important Python features and improve your skills. There are many paths to Rome! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f3db.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2>Method 2: Unpacking</h2>
<p class="has-global-color-8-background-color has-background">An easy and Pythonic way to print a dictionary without the <code>dict_values</code> prefix is to <a href="https://blog.finxter.com/python-unpacking/" data-type="post" data-id="396420" target="_blank" rel="noreferrer noopener">unpack</a> all values into the <code>print()</code> function using the <a href="https://blog.finxter.com/what-is-asterisk-in-python/" data-type="post" data-id="1344" target="_blank" rel="noreferrer noopener">asterisk operator</a>. This works because the <code>print()</code> function allows an arbitrary number of values as input. It prints those values separated by a single whitespace character per default.</p>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(*my_dict.values())
# Carl 42 100000
</pre>
<p>It cannot get any more concise, frankly. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>Of course, you can change the separator and end arguments accordingly to obtain more control of the output:</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="">my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(*my_dict.values(), sep='\n', end='\nThe End')</pre>
<p>Output:</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="">Carl
42
100000
The End</pre>
<p>Do you need even greater flexibility than this? No problem! See here: <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2935.png" alt="⤵" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2>Method 3: String Join Function and Generator Expression</h2>
<p class="has-global-color-8-background-color has-background">To convert the dictionary values to a single string object without <code>'dict_values'</code> in it and with maximal control, you can use the <code><a href="https://blog.finxter.com/python-string-join/" data-type="post" data-id="26062" target="_blank" rel="noreferrer noopener">string.join()</a></code> function in combination with a <a href="https://blog.finxter.com/understanding-generators-in-python/" data-type="post" data-id="33873" target="_blank" rel="noreferrer noopener">generator expression</a> and the built-in <code><a href="https://blog.finxter.com/python-str-function/" data-type="post" data-id="23735" target="_blank" rel="noreferrer noopener">str()</a></code> function. </p>
<p>Here’s an example:</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="">my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(', '.join(str(x) for x in my_dict.values()))
# Carl, 42, 100000</pre>
<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>: You can replace the comma <code>','</code> with your desired separator character and modify the representation of each individual element by modifying the expression <code>str(x)</code> of the generator expression to something arbitrary complicated.</p>
<p>See here for something crazy that wouldn’t make any sense:</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="">my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(' | '.join('x' + str(x) + 'x' for x in my_dict.values()))
# xCarlx | x42x | x100000x</pre>
<p>Note that you could also use the <code><a href="https://blog.finxter.com/python-repr-function/" data-type="post" data-id="23817" target="_blank" rel="noreferrer noopener">repr()</a></code> function instead of the <code>str()</code> function in this example—it wouldn’t matter too much.</p>
<p>Finally, I’d recommend you check out this tutorial to learn more how generator expressions work—many Python beginners struggle with this concept even though it’s ubiquitous in expert coders’ code bases. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<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 href="https://blog.finxter.com/python-one-line-generator/" data-type="post" data-id="13194" target="_blank" rel="noreferrer noopener">Understanding One-Line Generators in Python</a></p>
</div>


https://www.sickgaming.net/blog/2022/10/...ct_values/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016