I've been trying to print a list of the book titles within the named tuple in alphabetical order, but I don't know what I'm doing wrong. I've set up my named tuple as so:

Book = namedtuple('Book', 'author title genre year price instock')
# Book attributes: author, title of book, category of book, year of publication, and number of copies available
BSI = [
Book('George Orwell', 'Animal Farm', 'Fiction', 1945, 9.99, 21),
Book('J.K. Rowling', 'Harry Potter and the Deathly Hallows', 'Fantasy', 2007, 24.26, 32),
Book('J.R.R. Tolkien', 'The Fellowship of the Ring', 'Fantasy', 1954, 10.87, 26),
Book('Toni Morrison', 'The Bluest Eye', 'Fiction', 1970, 11.02, 13),
Book('Ernesto Galarza', 'Barrio Boy', 'Autobiography', 1971, 13.50, 23),
Book('Stephen Chbosky', 'The Perks of Being a Wallflower', 'Fiction', 1999, 8.01, 25)]

And I'm trying to create several statements that will print the titles in alphabetical order without affecting the original order of the list. So far I've been trying to play around with this:

for Book.title in BSI:
print(sorted(Book.title))

I've tried several other ways, but I've had no luck. Please explain my mistake, I'd like to know what I'm doing wrong. Thanks!

1 answer

Nevermind, figured it out! :D