Skip to content

Instantly share code, notes, and snippets.

@raeq
Created May 7, 2020 16:00
Show Gist options
  • Save raeq/66abead8de5dc48055a272ea54ca93a5 to your computer and use it in GitHub Desktop.
Save raeq/66abead8de5dc48055a272ea54ca93a5 to your computer and use it in GitHub Desktop.
Dictionary Example
"""
A module illsutrating storing a key-value pair in a dictionary.
"""
import collections
Book: collections.namedtuple = collections.namedtuple("Book", ["isbn", "title"])
books_as_list: list = list()
books_as_dict: dict = dict()
books_as_list.append(
Book(isbn="1510752242", title="Restoring Faith in the Promise of Science")
)
books_as_list.append(
Book(title="Midnight Sun", isbn="031670704X")
)
books_as_list.append(
Book(isbn="1524763136", title="Becoming")
)
books_as_list.append(
Book(isbn="0735219095", title="Where the Crawdads Sing")
)
books_as_list.append(
Book(isbn="1338635174", title="The Ballad of Songbirds and Snakes")
)
for book in books_as_list:
key, value = book.isbn, book.title
books_as_dict[key] = value
print(f"List of books: {books_as_list}\n")
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