Python print dictionary values | Example code

Use values() method to print dictionary values in Python. This is an inbuilt method that returns a list of all the values available in a given dictionary.

Syntax

dictionary.values()

Python print dictionary values example

Simple example code. This method will return a view object that displays a list of all the values in the dictionary.

dict1 = {"A": 2, "B": 3, "C": 4}

print(dict1.values())

Output:

Python print dictionary values

Using for loop & dict.items()

This way you can get dictionary values without object view.

dict1 = {"A": 2, "B": 3, "C": 4}

for key, value in dict1.items():
    print(dict1[key])

OR

Print a dictionary values line by line by iterating over keys

dict1 = {"A": 2, "B": 3, "C": 4}

for key in dict1:
    print(dict1[key])

Output:

2
3
4

Using List Comprehension

In a single line using list comprehension & dict.items().

dict1 = {"A": 2, "B": 3, "C": 4}

res = [values for key, values in dict1.items()]
print(res)

Output:

[2, 3, 4]

Do comment if you have any doubts and suggestions on this Python dictionary tutorial.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.