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”

5/5 – (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(...), for example: dict_values([1, 2, 3]).

Here’s an example:

my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(my_dict.values())
# dict_values(['Carl', 42, 100000])

There are multiple ways to change the string representation of the values, so that the print() output doesn’t yield the strange dict_values view object.

Method 1: Convert to List


An easy way to obtain a pretty output when printing the dictionary values without dict_values(...) representation is to convert the dict_value object to a list using the list() built-in function. For instance, print(list(my_dict.value())) prints the dictionary values as a simple list.

Here’s an example:

my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(list(my_dict.values()))
# ['Carl', 42, 100000]

So far, so simple. Read on to learn or recap some important Python features and improve your skills. There are many paths to Rome! ?

Method 2: Unpacking


An easy and Pythonic way to print a dictionary without the dict_values prefix is to unpack all values into the print() function using the asterisk operator. This works because the print() function allows an arbitrary number of values as input. It prints those values separated by a single whitespace character per default.

Here’s an example:

my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(*my_dict.values())
# Carl 42 100000

It cannot get any more concise, frankly. ?

Of course, you can change the separator and end arguments accordingly to obtain more control of the output:

my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(*my_dict.values(), sep='\n', end='\nThe End')

Output:

Carl
42
100000
The End

Do you need even greater flexibility than this? No problem! See here: ⤵

Method 3: String Join Function and Generator Expression


To convert the dictionary values to a single string object without 'dict_values' in it and with maximal control, you can use the string.join() function in combination with a generator expression and the built-in str() function.

Here’s an example:

my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(', '.join(str(x) for x in my_dict.values()))
# Carl, 42, 100000

? Note: You can replace the comma ',' with your desired separator character and modify the representation of each individual element by modifying the expression str(x) of the generator expression to something arbitrary complicated.

See here for something crazy that wouldn’t make any sense:

my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(' | '.join('x' + str(x) + 'x' for x in my_dict.values()))
# xCarlx | x42x | x100000x

Note that you could also use the repr() function instead of the str() function in this example—it wouldn’t matter too much.

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. ?

? Recommended Tutorial: Understanding One-Line Generators in Python



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



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Dictionary of Lists to DataFrame – Python Conversion xSicKxBot 0 1,374 04-17-2023, 03:46 AM
Last Post: xSicKxBot
  [Tut] How I Built an English Dictionary App with Django xSicKxBot 0 1,544 03-29-2023, 04:46 AM
Last Post: xSicKxBot
  [Tut] How to Print a NumPy Array Without Scientific Notation in Python xSicKxBot 0 1,216 10-20-2022, 11:44 AM
Last Post: xSicKxBot
  [Tut] How to Count the Number of Unique Values in a List in Python? xSicKxBot 0 1,346 10-19-2022, 03:08 AM
Last Post: xSicKxBot
  [Tut] Can a Python Dictionary Have a List as a Value? xSicKxBot 0 1,156 10-15-2022, 08:22 AM
Last Post: xSicKxBot
  [Tut] Python Print Dictionary Without One Key or Multiple Keys xSicKxBot 0 1,250 10-10-2022, 08:56 PM
Last Post: xSicKxBot
  [Tut] How to Print a List Without Commas in Python xSicKxBot 0 1,224 10-08-2022, 08:06 AM
Last Post: xSicKxBot
  [Tut] Python Print List Without Truncating xSicKxBot 0 1,359 10-07-2022, 12:21 PM
Last Post: xSicKxBot
  [Tut] How to Get a Random Entry from a Python Dictionary xSicKxBot 0 1,256 09-15-2022, 02:08 PM
Last Post: xSicKxBot
  [Tut] How to Find the Most Common Element in a Python Dictionary xSicKxBot 0 1,091 09-09-2022, 12:16 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016