{"id":128750,"date":"2022-10-09T13:48:44","date_gmt":"2022-10-09T13:48:44","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=769158"},"modified":"2022-10-09T13:48:44","modified_gmt":"2022-10-09T13:48:44","slug":"python-print-dictionary-without-one-key-or-multiple-keys","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/09\/python-print-dictionary-without-one-key-or-multiple-keys\/","title":{"rendered":"Python Print Dictionary Without One Key or Multiple Keys"},"content":{"rendered":"\n<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;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<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>\n<p>There are multiple ways to accomplish this and I&#8217;ll show you the best ones in this tutorial. Let&#8217;s get started! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f680.png\" alt=\"\ud83d\ude80\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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>\n<h2>Method 1: Dictionary Comprehension<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" 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\/uploads\/2022\/10\/image-55-300x200.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-55-768x512.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-55.png 1407w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<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>\n<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&#8217;s key-value pairs and confirms for each key that it doesn&#8217;t belong to the ones that should be ignored. <\/p>\n<p>Here&#8217;s a minimal example:<\/p>\n<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'}\nmy_dict = {'x': 1, 'y': 2, 'z': 3} filtered_dict = {k:v for k,v in my_dict.items() if k not in ignore_keys}\nprint(filtered_dict)\n# {'z': 3}<\/pre>\n<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>\n<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&#8217;t belong to the set. <\/p>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" 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>\n<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>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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>\n<h2>Method 2: Simple For Loop with If Condition<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" 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\/uploads\/2022\/10\/image-56-300x200.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-56-768x512.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-56.png 1407w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<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>\n<p>Here&#8217;s an example using three lines and directly printing the key-value pairs:<\/p>\n<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'}\nmy_dict = {'x': 1, 'y': 2, 'z': 3} for k, v in my_dict.items(): if k not in ignore_keys: print(k, v)\n<\/pre>\n<p>The output:<\/p>\n<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>\n<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>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>My Recommendation &#8211; Use This Method!<\/h2>\n<p>I could have listed many more ways to solve this problem of printing a dict except one or more keys. <\/p>\n<p>I have seen super inefficient ways proposed on forums that use <code>exclude_keys<\/code> that are list types. <\/p>\n<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>\n<p class=\"has-global-color-8-background-color has-background\">But I don&#8217;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>\n<p>That&#8217;s it. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f44c.png\" alt=\"\ud83d\udc4c\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) 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 print() function. There are multiple ways to accomplish this and I&#8217;ll show you the best ones in this tutorial. Let&#8217;s get started! Recommended [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-128750","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128750","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=128750"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128750\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}