Skip to content

Instantly share code, notes, and snippets.

@SuvroBaner
Created December 12, 2018 13:59
Show Gist options
  • Save SuvroBaner/476b66f6ccfc11e2ec8abd5af336a6f4 to your computer and use it in GitHub Desktop.
Save SuvroBaner/476b66f6ccfc11e2ec8abd5af336a6f4 to your computer and use it in GitHub Desktop.
books = ['The Lord of the Rings', 'The Hobbit', 'Harry Potter and the Chamber of Secrets',
'Black Beauty', 'Kane and Abel', 'To Kill a Mocking Bird', 'Gitanjali',
'Spring and Autumn Annals', 'Rumi Poems'] # defining a list of books
book_catalog = {} # defining a dictionary
for book in books:
book_index = book[0] # extract the first letter (for catalog)
if book_index not in book_catalog:
book_catalog[book_index] = [book]
else:
book_catalog[book_index].append(book) # appending the book if the index already exists in the catalog
book_catalog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment