Created
May 7, 2020 16:38
-
-
Save raeq/bba7021a42b6e0fb3644b8696bc59060 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A module illsutrating storing a key-value pair in a dictionary. | |
""" | |
import collections | |
import json | |
Book: collections.namedtuple = collections.namedtuple("Book", ["isbn", "title", "author"]) | |
books_as_list: list = list() | |
books_as_dict: dict = dict() | |
books_as_list.append( | |
Book(isbn="1510752242", title="Restoring Faith in ... of Science", author="K Heckenlively") | |
) | |
books_as_list.append( | |
Book(title="Midnight Sun", isbn="031670704X", author="S Meyer") | |
) | |
books_as_list.append( | |
Book(isbn="1524763136", title="Becoming", author="M Obama") | |
) | |
books_as_list.append( | |
Book(isbn="0735219095", title="Where the Crawdads Sing", author=" S Owens") | |
) | |
books_as_list.append( | |
Book(isbn="1338635174", title="The Ballad of Songbirds and Snakes", author="S Collins") | |
) | |
for book in books_as_list: | |
key, value = book.isbn, {'title' : book.title, 'author': book.author} | |
books_as_dict[key] = value | |
print(f"Dict of books: {books_as_dict}\n") | |
print(json.dumps(books_as_dict)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment