Print all values in a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Sep 16, 2022

banner

Photo from Unsplash

Print all values in a dictionary in Python #

To print all values in a dictionary:

  1. Use the dict.values() method to get a view of the dictionary's values.
  2. Use a for loop to iterate over the view.
  3. Use the print() function to print each value.
main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } values = list(my_dict.values()) print(values) # 👉️ ['bobbyhadz', 30, 'bobbyhadz.com'] for key, value in my_dict.items(): # name bobbyhadz # age 30 # website bobbyhadz.com print(key, value) for key in my_dict.values(): # bobbyhadz # 30 # bobbyhadz.com print(key)

We used a for loop to print all values in a dictionary.

main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } for key in my_dict.values(): # bobbyhadz # 30 # bobbyhadz.com print(key)

The dict.values method returns a new view of the dictionary's values.

main.py
my_dict = {'id': 1, 'name': 'bobbyhadz'} print(my_dict.values()) # 👉️ dict_values([1, 'bobbyhadz'])
If you need to print all key-value pairs in the dictionary, use the dict.items() method.
main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } for key, value in my_dict.items(): # name bobbyhadz # age 30 # website bobbyhadz.com print(key, value)

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

main.py
my_dict = {'id': 1, 'name': 'BobbyHadz'} print(my_dict.items()) # 👉️ dict_items([('id', 1), ('name', 'BobbyHadz')])

Use a formatted string literal if you need to format the values in any way when printing them.

main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } for key, value in my_dict.items(): # name=bobbyhadz # age=30 # website=bobbyhadz.com print(f'{key}={value}')
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
var1 = 'bobby' var2 = 'hadz' result = f'Name: {var1}{var2}' print(result) # 👉️ Name: bobbyhadz

Make sure to wrap expressions in curly braces - {expression}.

If you need to get a list of the dictionary's values, use the dict.values() method.

main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } values = list(my_dict.values()) print(values) # 👉️ ['bobbyhadz', 30, 'bobbyhadz.com'] keys = list(my_dict.keys()) print(keys) # 👉️ ['name', 'age', 'website']

The dict.values method returns a new view of the dictionary's values.

The dict.keys method returns a new view of the dictionary's keys.

Make sure to use the list() class if you need to convert the view objects to a list.

If you need to join the dictionary's values into a string, use the str.join() method.

main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } values = ', '.join(str(value) for value in my_dict.values()) print(values) # 👉️ bobbyhadz, 30, bobbyhadz.com keys = ', '.join(my_dict.keys()) print(keys) # 👉️ name, age, website items = ', '.join(f'{key}: {value}' for key, value in my_dict.items()) print(items) # 👉️ name: bobbyhadz, age: 30, website: bobbyhadz.com

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

Our dictionary has an integer value, so we had to use the str() class to convert each value to a string before calling str.join().

The string the method is called on is used as the separator between the elements.

We used a comma and a space as the separator, but you can use any other delimiter.

Here is an example that uses a newline (\n) character as the delimiter.

main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } values = '\n'.join(str(value) for value in my_dict.values()) # bobbyhadz # 30 # bobbyhadz.com print(values) # 👉️ bobbyhadz, 30, bobbyhadz.com items = '\n'.join(f'{key}: {value}' for key, value in my_dict.items()) # name: bobbyhadz # age: 30 # website: bobbyhadz.com print(items)

When the values in the dictionary are joined with a newline (\n) character separator, they are printed on separate lines.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.