Skip to content

Instantly share code, notes, and snippets.

@raeq
Created May 7, 2020 16:26
Show Gist options
  • Save raeq/7384ac506191f283ce59fe4f99126bb2 to your computer and use it in GitHub Desktop.
Save raeq/7384ac506191f283ce59fe4f99126bb2 to your computer and use it in GitHub Desktop.
"""
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, {"title" : book.title, "author" : 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