Print a dictionary in one line in Python

avatar

Borislav Hadzhiev

Last updated: Sep 15, 2022

banner

Photo from Unsplash

Print a dictionary in one line in Python #

To print a dictionary in one line:

  1. Format the items of the dictionary into strings.
  2. Use the str.join() method to join the strings into a single string.
  3. Use the print() function to print the string.
main.py
my_dict = {'id': 1, 'name': 'bobbyhadz', 'salary': 50} # ✅ print dictionary in one line result = ', '.join(f'{key}: {value}' for key, value in my_dict.items()) print(result) # 👉️ id: 1, name: bobbyhadz, salary: 50 # -------------------------------------------- # ✅ print dictionary with each key-value pair on a separate line result = '\n'.join(f'{key}: {value}' for key, value in my_dict.items()) # id: 1 # name: bobbyhadz # salary: 50 print(result)

We used the str.join() method to print a dictionary in one line.

We passed a generator expression to the str.join() method.

main.py
my_dict = {'id': 1, 'name': 'bobbyhadz', 'salary': 50} result = ', '.join(f'{key}: {value}' for key, value in my_dict.items()) print(result) # 👉️ id: 1, name: bobbyhadz, salary: 50
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use a formatted string literal to format the key and value into a string.

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'{var1}{var2}' print(result) # 👉️ bobbyhadz

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

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

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

We used a comma and a space separator to join the formatted strings but you can use any other delimiter.

main.py
my_dict = {'id': 1, 'name': 'bobbyhadz', 'salary': 50} result = ' | '.join(f'{key}: {value}' for key, value in my_dict.items()) print(result) # 👉️ id: 1 | name: bobbyhadz | salary: 50

If you need to print each key-value pair on separate lines, use a newline (\n) character as the delimiter.

main.py
my_dict = {'id': 1, 'name': 'bobbyhadz', 'salary': 50} result = '\n'.join(f'{key}: {value}' for key, value in my_dict.items()) # id: 1 # name: bobbyhadz # salary: 50 print(result)

Alternatively, you can use the str.format() method.

main.py
my_dict = {'id': 1, 'name': 'bobbyhadz', 'salary': 50} result = ', '.join('{}: {}'.format(key, value) for key, value in my_dict.items()) print(result) # 👉️ id: 1, name: bobbyhadz, salary: 50

The str.format method performs string formatting operations.

main.py
first = 'Bobby' last = 'Hadz' result = "Employee name: {} {}".format(first, last) print(result) # 👉️ "Employee name: Bobby Hadz"

The string the method is called on can contain replacement fields specified using curly braces {}.

Make sure to provide exactly as many arguments to the format() method as you have replacement fields in the string.

Which approach you pick is a matter of personal preference. I'd go with using a formatted string literal because they're easier to read.

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.