Created
May 7, 2020 16:00
-
-
Save raeq/66abead8de5dc48055a272ea54ca93a5 to your computer and use it in GitHub Desktop.
Dictionary Example
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"]) | |
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