Created
May 7, 2020 16:20
-
-
Save raeq/4b100327908ddf16fdd28e81586bf68e 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 | |
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, (book.title, book.author) | |
books_as_dict[key] = value | |
print(f"Dict of books: {books_as_dict}\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment