10-12-2022, 02:48 AM
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, Python returns a object, a view of the dictionary values. The representation prints the keys enclosed in a weird , for example: .
Here’s an example:
![[Image: image-57.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-57.png)
There are multiple ways to change the string representation of the values, so that the output doesn’t yield the strange view object.
Method 1: Convert to List
An easy way to obtain a pretty output when printing the dictionary values without representation is to convert the object to a list using the built-in function. For instance, prints the dictionary values as a simple list.
Here’s an example:
So far, so simple. Read on to learn or recap some important Python features and improve your skills. There are many paths to Rome!![[Image: 1f3db.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f3db.png)
Method 2: Unpacking
An easy and Pythonic way to print a dictionary without the prefix is to unpack all values into the function using the asterisk operator. This works because the 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:
It cannot get any more concise, frankly.![[Image: 1f642.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png)
Of course, you can change the separator and end arguments accordingly to obtain more control of the output:
Output:
Do you need even greater flexibility than this? No problem! See here:![[Image: 2935.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/2935.png)
Method 3: String Join Function and Generator Expression
To convert the dictionary values to a single string object without in it and with maximal control, you can use the function in combination with a generator expression and the built-in function.
Here’s an example:
Note: You can replace the comma with your desired separator character and modify the representation of each individual element by modifying the expression of the generator expression to something arbitrary complicated.
See here for something crazy that wouldn’t make any sense:
Note that you could also use the function instead of the 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.![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
Recommended Tutorial: Understanding One-Line Generators in Python
5/5 – (1 vote)
Problem Formulation and Solution Overview
If you print all values from a dictionary in Python using
Code:
print(dict.values())Code:
dict_valuesCode:
dict_values(...)Code:
dict_values([1, 2, 3])Here’s an example:
Code:
my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(my_dict.values())
# dict_values(['Carl', 42, 100000])![[Image: image-57.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-57.png)
There are multiple ways to change the string representation of the values, so that the
Code:
print()Code:
dict_valuesMethod 1: Convert to List
An easy way to obtain a pretty output when printing the dictionary values without
Code:
dict_values(...)Code:
dict_valueCode:
list()Code:
print(list(my_dict.value()))Here’s an example:
Code:
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!
![[Image: 1f3db.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f3db.png)
Method 2: Unpacking
An easy and Pythonic way to print a dictionary without the
Code:
dict_valuesCode:
print()Code:
print()Here’s an example:
Code:
my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(*my_dict.values())
# Carl 42 100000It cannot get any more concise, frankly.
![[Image: 1f642.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png)
Of course, you can change the separator and end arguments accordingly to obtain more control of the output:
Code:
my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(*my_dict.values(), sep='\n', end='\nThe End')Output:
Code:
Carl
42
100000
The EndDo you need even greater flexibility than this? No problem! See here:
![[Image: 2935.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/2935.png)
Method 3: String Join Function and Generator Expression
To convert the dictionary values to a single string object without
Code:
'dict_values'Code:
string.join()Code:
str()Here’s an example:
Code:
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 Code:
','Code:
str(x)See here for something crazy that wouldn’t make any sense:
Code:
my_dict = {'name': 'Carl', 'age': 42, 'income': 100000}
print(' | '.join('x' + str(x) + 'x' for x in my_dict.values()))
# xCarlx | x42x | x100000xNote that you could also use the
Code:
repr()Code:
str()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.
![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
Recommended Tutorial: Understanding One-Line Generators in Python

