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!

Nevermind, figured it out! :D

The issue you're facing is related to how you're trying to access the title attribute of the named tuple objects. Instead of accessing it through the class name, which is `Book`, you should access it through the individual instances of the named tuple.

To print the book titles in alphabetical order without affecting the original order of the list, you need to follow these steps:

1. Extract the titles from the named tuple objects using a list comprehension.
2. Sort the list of titles.
3. Iterate over the sorted titles and print them.

Here's the corrected code:

```python
for book in sorted(BSI, key=lambda x: x.title):
print(book.title)
```

Explanation:

1. The `sorted` function is used to sort the list of named tuples (`BSI`) based on the `title` attribute. The `key` argument is used to specify how the sorting should be done, in this case, based on the `title` attribute.
2. The `lambda` function `lambda x: x.title` is used as the sorting key, which extracts the `title` attribute of each book object.
3. The sorted list of books is then iterated over, and the `title` attribute of each book is printed.

This will print the book titles in alphabetical order without modifying the original order of the list.