Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Print Dictionary Without One Key or Multiple Keys

#1
Python Print Dictionary Without One Key or Multiple Keys

<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;769158&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>
<p class="has-global-color-8-background-color has-background">The most Pythonic way to print a dictionary except for one or multiple keys is to filter it using dictionary comprehension and pass the filtered dictionary into the <code><a href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">print()</a></code> function. </p>
<p>There are multiple ways to accomplish this and I’ll show you the best ones in this tutorial. Let’s get started! <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;" /> </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/how-to-filter-a-dictionary-in-python/" data-type="post" data-id="8770" target="_blank" rel="noreferrer noopener">How to Filter a Dictionary in Python</a></p>
<h2>Method 1: Dictionary Comprehension</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="683" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-55-1024x683.png" alt="" class="wp-image-769367" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-55-1024x683.png 1024w, https://blog.finxter.com/wp-content/uplo...00x200.png 300w, https://blog.finxter.com/wp-content/uplo...68x512.png 768w, https://blog.finxter.com/wp-content/uplo...age-55.png 1407w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>Say, you have one or more keys stored in a variable <code>ignore_keys</code> that may be a <a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank" rel="noreferrer noopener">list</a> or a <a href="https://blog.finxter.com/sets-in-python/" data-type="post" data-id="1908" target="_blank" rel="noreferrer noopener">set</a> for efficiency reasons. </p>
<p class="has-global-color-8-background-color has-background">Create a filtered dictionary without one or multiple keys using the dictionary comprehension <code>{k:v for k,v in my_dict.items() if k not in ignore_keys}</code> that iterates over the original dictionary’s key-value pairs and confirms for each key that it doesn’t belong to the ones that should be ignored. </p>
<p>Here’s a minimal 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="">ignore_keys = {'x', 'y'}
my_dict = {'x': 1, 'y': 2, 'z': 3} filtered_dict = {k:v for k,v in my_dict.items() if k not in ignore_keys}
print(filtered_dict)
# {'z': 3}</pre>
<p>The <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-dict-items-method/" data-type="post" data-id="37673" target="_blank">dict.items()</a></code> method creates an <a rel="noreferrer noopener" href="https://blog.finxter.com/iterators-iterables-and-itertools/" data-type="post" data-id="29507" target="_blank">iterable</a> of key-value pairs over which we can iterate.</p>
<p>The <a rel="noreferrer noopener" href="https://blog.finxter.com/python-membership-not-in-operator/" data-type="post" data-id="34063" target="_blank">membership operator</a> <code>k not in ignore_keys</code> tests if a given key doesn’t belong to the set. </p>
<p class="has-global-color-8-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;" /> The <a rel="noreferrer noopener" href="https://blog.finxter.com/sets-in-python/" data-type="post" data-id="1908" target="_blank">runtime complexity</a> of the membership check is constant O(1) if you use a set for the <code>ignore_keys</code> data structure. It would be <a rel="noreferrer noopener" href="https://blog.finxter.com/runtime-complexity-of-python-list-methods-easy-table-lookup/" data-type="post" data-id="8723" target="_blank">linear</a> O(n) in the number of elements if you used a list which is not a good idea for that reason.</p>
<p>Note that you can also use this approach to <strong><em>print a dictionary except a single key</em></strong> by putting only one key into the ignore list.</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-dictionary-comprehension/" data-type="post" data-id="13313" target="_blank" rel="noreferrer noopener">Dictionary Comprehension in Python</a></p>
<h2>Method 2: Simple For Loop with If Condition</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="683" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-56-1024x683.png" alt="" class="wp-image-769370" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-56-1024x683.png 1024w, https://blog.finxter.com/wp-content/uplo...00x200.png 300w, https://blog.finxter.com/wp-content/uplo...68x512.png 768w, https://blog.finxter.com/wp-content/uplo...age-56.png 1407w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p class="has-global-color-8-background-color has-background">A not-so-Pythonic but reasonably readable way to print a dict without one or multiple keys is to use a simple <code>for</code> loop with <code>if</code> condition to avoid all keys in the ignore list.</p>
<p>Here’s an example using three lines and directly printing the key-value pairs:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4-6" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">ignore_keys = {'x', 'y'}
my_dict = {'x': 1, 'y': 2, 'z': 3} for k, v in my_dict.items(): if k not in ignore_keys: print(k, v)
</pre>
<p>The 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="">z 3</pre>
<p>Of course, you can modify the output to your own needs. See the customizations of the built-in <code>print()</code> function and its awesome arguments:</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-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">Python <code>print()</code> and Separator and End Arguments</a></p>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>My Recommendation – Use This Method!</h2>
<p>I could have listed many more ways to solve this problem of printing a dict except one or more keys. </p>
<p>I have seen super inefficient ways proposed on forums that use <code>exclude_keys</code> that are list types. </p>
<p>I have also seen elaborate schemes to use <a href="https://blog.finxter.com/python-set-difference/" data-type="post" data-id="28030" target="_blank" rel="noreferrer noopener">set difference</a> operations or more. </p>
<p class="has-global-color-8-background-color has-background">But I don’t recommend anything else than <strong>dict comprehension</strong> if you want to create a filtered dictionary object first and the <strong>simple <code>for</code> loop</strong> if you want to print on the fly. </p>
<p>That’s it. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f44c.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
</div>


https://www.sickgaming.net/blog/2022/10/...iple-keys/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016