{"id":128771,"date":"2022-10-10T14:27:59","date_gmt":"2022-10-10T14:27:59","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=772646"},"modified":"2022-10-10T14:27:59","modified_gmt":"2022-10-10T14:27:59","slug":"python-print-dictionary-values-without-dict_values","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/10\/python-print-dictionary-values-without-dict_values\/","title":{"rendered":"Python Print Dictionary Values Without \u201cdict_values\u201d"},"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;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;}\">\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<h2>Problem Formulation and Solution Overview<\/h2>\n<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>\n<p>Here&#8217;s an example:<\/p>\n<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}\nprint(my_dict.values())\n# dict_values(['Carl', 42, 100000])<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" 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\/uploads\/2022\/10\/image-57-300x78.png 300w\" sizes=\"auto, (max-width: 701px) 100vw, 701px\" \/><\/figure>\n<\/div>\n<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&#8217;t yield the strange <code>dict_values<\/code> view object. <\/p>\n<\/p>\n<h2>Method 1: Convert to List<\/h2>\n<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>\n<p>Here&#8217;s an example:<\/p>\n<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}\nprint(list(my_dict.values()))\n# ['Carl', 42, 100000]<\/pre>\n<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 decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f3db.png\" alt=\"\ud83c\udfdb\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Method 2: Unpacking<\/h2>\n<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>\n<p>Here&#8217;s an example:<\/p>\n<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}\nprint(*my_dict.values())\n# Carl 42 100000\n<\/pre>\n<p>It cannot get any more concise, frankly. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p>Of course, you can change the separator and end arguments accordingly to obtain more control of the output:<\/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=\"\">my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}\nprint(*my_dict.values(), sep='\\n', end='\\nThe End')<\/pre>\n<p>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=\"\">Carl\n42\n100000\nThe End<\/pre>\n<p>Do you need even greater flexibility than this? No problem! See here: <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2935.png\" alt=\"\u2935\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Method 3: String Join Function and Generator Expression<\/h2>\n<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>\n<p>Here&#8217;s an 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=\"\">my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}\nprint(', '.join(str(x) for x in my_dict.values()))\n# Carl, 42, 100000<\/pre>\n<p class=\"has-base-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;\" \/> <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>\n<p>See here for something crazy that wouldn&#8217;t make any sense:<\/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=\"\">my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}\nprint(' | '.join('x' + str(x) + 'x' for x in my_dict.values()))\n# xCarlx | x42x | x100000x<\/pre>\n<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&#8212;it wouldn&#8217;t matter too much.<\/p>\n<p>Finally, I&#8217;d recommend you check out this tutorial to learn more how generator expressions work&#8212;many Python beginners struggle with this concept even though it&#8217;s ubiquitous in expert coders&#8217; code bases. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" 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\/python-one-line-generator\/\" data-type=\"post\" data-id=\"13194\" target=\"_blank\" rel=\"noreferrer noopener\">Understanding One-Line Generators in Python<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation and Solution Overview If you print all values from a dictionary in Python using print(dict.values()), Python returns a dict_values object, a view of the dictionary values. The representation prints the keys enclosed in a weird dict_values(&#8230;), for example: dict_values([1, 2, 3]). Here&#8217;s an example: my_dict = {&#8216;name&#8217;: &#8216;Carl&#8217;, &#8216;age&#8217;: [&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-128771","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\/128771","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=128771"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128771\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}