How to print a dictionary on separate lines

Question:

I am attempting to use the following approach to printing the contents of a dictionary which consists of a key and multiple values for each item.


for target in items:

   print("{0}:{1}".format(target, items[target]))

The current output is the "raw" dictionary contents on a single line and I’m wondering how to print the values on separate lines instead. I have been able to print the first key as desired, but not the subsequent values.

EDIT

Here is the structure of the dictionary with the format of the desired output:

Dictionary format:

books: {56123: {‘Title’: ‘Book1’, ‘Author’: ‘Test Author’, ‘Publisher’: ‘Penguin Publishing’, ‘Pages’: ‘340’, ‘Year’: ‘2011’, ‘Copies’: 5, ‘Available’: 5, ‘ID’: 56123}, 23165: {‘Title’: ‘Book2’, ‘Author’: ‘Author Test’, ‘Publisher’: ‘Printing Press’, ‘Pages’: ‘912’, ‘Year’: ‘2021’, ‘Copies’: 4, ‘Available’: 4, ‘ID’: 23165}…

movies: {90011: {‘Title’: ‘Movie1’, ‘Director’: ‘Test Director’, ‘Length’: ‘140’, ‘Genre’: ‘Thriller’, ‘Year’: ‘2021’, ‘Copies’: 3, ‘Available’: 3, ‘ID’: 90011}, 34125: {‘Title’: ‘Movie 2’, ‘Director’: ‘Test Director 2’, ‘Length’: ‘240’, ‘Genre’: ‘Action’, ‘Year’: ‘2022’, ‘Copies’: 2, ‘Available’: 2, ‘ID’: 34125}….

Desired output:

ID: 56123

Title: Book1

Year: 2011

Copies: 5

Available: 5

Author: Test Author

Publisher: Penguin Publishing

Pages: 340

Asked By: BL Eaker

||

Answers:

Try using the json.dumps function.

import json

a = {17104: {'Title': 'A River', 'Author': 'Elisha Mitchell', 'Publisher': 'FPG Publishing', 'Pages': '345', 'Year': '2014', 'Copies': 2, 'Available': 2, 'ID': 17104}, 37115: {'Title': 'Aim High', 'Author': 'George Tayloe Winston', 'Publisher': 'Manning Hall Press', 'Pages': '663', 'Year': '2014', 'Copies': 5, 'Available': 5, 'ID': 37115}}

b = json.dumps(a, indent=4)
print(b)

Output:

{
    "17104": {
        "Title": "A River",
        "Author": "Elisha Mitchell",
        "Publisher": "FPG Publishing",
        "Pages": "345",
        "Year": "2014",
        "Copies": 2,
        "Available": 2,
        "ID": 17104
    },
    "37115": {
        "Title": "Aim High",
        "Author": "George Tayloe Winston",
        "Publisher": "Manning Hall Press",
        "Pages": "663",
        "Year": "2014",
        "Copies": 5,
        "Available": 5,
        "ID": 37115
    }
}
Answered By: Alexander

If you just want more readable output I would suggest looking at pprint. You can add formatting options, but this is a basic example:

from pprint import pprint

items = {'first': {'a': 1, 'b': 2, 'c': 3},'second': {'a': 1, 'b': 2, 'c': 3}, 'third': {'a': 1, 'b': 2, 'c': 3},'fourth': {'a': 1, 'b': 2, 'c': 3}}

pprint(items)

Otherwise, you can use print in nested for loop to drill down through the parts of the dictionary like this:

example = {'books': {56123: {'Title': 'Book1', 'Author': 'Test Author', 'Publisher': 'Penguin Publishing', 'Pages': '340', 'Year': '2011', 'Copies': 5, 'Available': 5, 'ID': 56123}, 23165: {'Title': 'Book2', 'Author': 'Author Test', 'Publisher': 'Printing Press', 'Pages': '912', 'Year': '2021', 'Copies': 4, 'Available': 4, 'ID': 23165}}, 'movies': {90011: {'Title': 'Movie1', 'Director': 'Test Director', 'Length': '140', 'Genre': 'Thriller', 'Year': '2021', 'Copies': 3, 'Available': 3, 'ID': 90011}, 34125: {'Title': 'Movie 2', 'Director': 'Test Director 2', 'Length': '240', 'Genre': 'Action', 'Year': '2022', 'Copies': 2, 'Available': 2, 'ID': 34125}}}

for category, entries in example.items():
    print(f"n**** {category} ****n")
   
    for entry_id, entry_values in entries.items():
        print(f"n- {entry_id} -n")
       
        for k, v in entry_values.items():
            print(f"{k}: {v}")

output:

**** books ****


- 56123 -

Title: Book1
Author: Test Author
Publisher: Penguin Publishing
Pages: 340
Year: 2011
Copies: 5
Available: 5
ID: 56123

- 23165 -

Title: Book2
Author: Author Test
Publisher: Printing Press
Pages: 912
Year: 2021
Copies: 4
Available: 4
ID: 23165

**** movies ****


- 90011 -

Title: Movie1
Director: Test Director
Length: 140
Genre: Thriller
Year: 2021
Copies: 3
Available: 3
ID: 90011

- 34125 -

Title: Movie 2
Director: Test Director 2
Length: 240
Genre: Action
Year: 2022
Copies: 2
Available: 2
ID: 34125

But if you don’t mind the json formatting, alexander’s answer is a lot simpler.

Answered By: shawn caza
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.