Home > Software design >  print a dictionary adding a blank space between each blocks
print a dictionary adding a blank space between each blocks

Time:11-06

New to python. Here's a nested dictionary with two books each having 8 attributes.

book_collection ={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}}

for id, book in book_collection.items():
    for book_attribute, attribute_value in book.items():
        print(book_attribute, ': ', attribute_value, sep='')

The output:

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

How can I add a blank space between each book, and bring the 'ID' attribute to the first row of each book. The output is supposed to look like this:

ID: 17104
Title: A River
Author: Elisha Mitchell
Publisher: FPG Publishing
Pages: 345
Year: 2014
Copies: 2
Available: 2

ID: 37115
Title: Aim High
Author: George Tayloe Winston
Publisher: Manning Hall Press
Pages: 663
Year: 2014
Copies: 5
Available: 5

If there are 20 books, how can I just print the first 10 and ask the user for permission to continue?

CodePudding user response:

Use this:

 for id, book in book_collection.items():
        for book_attribute, attribute_value in book.items():
            print(book_attribute, ': ', attribute_value, sep='')
        print()

You can just use the index() function to check if the index is 9 then ask like this

for id, book in book_collection.items():
    if book_collection.index(id) == 9:
        n = int(input("Press 0 to continue or else to exit"))
        if n != 0:
            break
    for book_attribute, attribute_value in book.items():
        print(book_attribute, ': ', attribute_value, sep='')
    print()

CodePudding user response:

A dictionary's items method method returns an iterable of tuples (immutable lists). Each tuple yielded represents a pair of key and value, with the key being in the tuple's 0 index, and the value in 1 index.

The for loop you're using - for book_attribute, attribute_value in book.items(): - is syntactic sugar for "take the two values in the tuple and assign them to these variables, then run the code in this block."

It might be easier to think of it this way:

>>> book_dict = {'Title': 'A River', 'Author': 'Elisha Mitchell', 'Publisher': 'FPG Publishing', 'Pages': '345', 'Year': '2014', 'Copies': 2, 'Available': 2, 'ID': 17104}
>>> book_dict_entries = list(book_dict.items())
>>> print(book_dict_entries)
[('Title', 'A River'), ('Author', 'Elisha Mitchell'), ('Publisher', 'FPG Publishing'), ('Pages', '345'), ('Year', '2014'), ('Copies', 2), ('Available', 2), ('ID', 17104)]

There's a few directions to go from here. One way is that - since it's just a list -you can search for the one representing the ID field and swap it with whatever happens to be the first element in that list. Or, before turning it into a list, simply print ID from the dictionary, then filter that field when enumerating through the rest of the fields.

As to your second question, if you want to print an empty line at a certain point - simply call print with no arguments. Like when you've finished printing each dictionary.

CodePudding user response:

I would define the dict with ID as the first key since from Python 3.7 (not before!) dicts are ordered

Put ID as the first key, and add print() after each inner loop.

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

for id, book in book_collection.items():
    for book_attribute, attribute_value in book.items():
        print(book_attribute, ': ', attribute_value, sep='')
    print()
  • Related