Please help me to debug this error

The program below is giving me this error, I don’t know why. Please help
Traceback (most recent call last):
File “C:/Users/Dalison Chimchere/Documents/sorting and”, line 73, in
File “C:/Users/Dalison Chimchere/Documents/sorting and”, line 69, in main
dark_side_index = albums2.index(Album(“Dark Side of the Moon”, None, None))
ValueError: <main.Album object at 0x0000019F0451E890> is not in list

This program defines the Album class with the specified attributes and string representation.
Then, it creates two lists albums1 and albums2, populates them with Album objects,
and performs various operations like sorting, swapping, copying, adding elements, and searching.
class Album: # defineing Album class
def init(self, album_name, number_of_songs, album_artist):
self.album_name = album_name
self.number_of_songs = number_of_songs
self.album_artist = album_artist

def __str__(self):  
   return f"({self.album_name}, {self.album_artist}, {self.number_of_songs})"

def main():
albums1 =
albums1.append(Album(“Thriller”, 9, “Michael Jackson”))
albums1.append(Album(“Back in Black”, 10, “AC/DC”))
albums1.append(Album(“The Dark Side of the Moon”, 10, “Pink Floyd”))
albums1.append(Album(“Rumours”, 11, “Fleetwood Mac”))
albums1.append(Album(“Born to Run”, 8, “Bruce Springsteen”))

# Print albums before sorting
print("Albums before sorting:")
for album in albums1:

# Swap elements at positions 1 and 2
albums1[1], albums1[2] = albums1[2], albums1[1]

# Print albums after swapping
print("\nAlbums after swapping positions 1 and 2:")
for album in albums1:

albums2 = []
albums2.append(Album("Purple Rain", 9, "Prince"))        
albums2.append(Album("Like a Prayer", 9, "Madonna"))
albums2.append(Album("Bridge Over Troubled Water", 11, "Simon & Garfunkel"))
albums2.append(Album("Achtung Baby", 11, "U2"))
albums2.append(Album("Nevermind", 14, "Nirvana"))

# Print albums2
for album in albums2:
# Copy all albums from albums1 to albums2

# Add additional albums
albums2.append(Album("Dark Side of the Moon", 9, "Pink Floyd"))
albums2.append(Album("Oops!... I Did It Again", 16, "Britney Spears"))

# Print albums2 after additions
print("\nAlbums2 after additions:")
for album in albums2:

# Sort albums2 alphabetically by album name
albums2.sort(key=lambda album: album.album_name)

# Print albums2 after sorting alphabetically
print("\nAlbums2 after sorting alphabetically:")
for album in albums2:

# Search for Dark Side of the Moon
dark_side_index = albums2.index(Album("Dark Side of the Moon", 9, "Pink Floyd"))
(f"\nIndex of Dark Side of the Moon: {dark_side_index}")

if name == “main”:


be sure to copy your ENTIRE code between two lines, each having three back ticks as shown in the following figure.


I did a quick scan of your code near the bottom. It should be:

if __name__ == '__main__':
    main()  # Has to be indented

The indentation was lost precisely due to the lack of formatting that you pointed out.

The real issue is because of how list.index works but I’ll let @kknechtel write the long answer :wink:

1 Like

Clearly it actually is indented in the actual code. Otherwise the error would be completely different.

For future questions, please take Paul’s advice, and also read the pinned thread for details.

But this is a very common problem.

The first step in debugging something is to track down where it happens.

The second step is to check if you understand it.

You already did those steps, and it seems you don’t understand it. So the next step is to try to reproduce the problem, as simply as possible. That works by trying to cut out parts that aren’t relevant to causing the problem, and checking that you can still make the same problem happen.

If you keep at that, you may get as far as just making the class and putting a single instance in a single list, and realizing you can’t “find” it with .index:

class Album:
    def __init__(self, album_name, number_of_songs, album_artist):
        self.album_name = album_name
        self.number_of_songs = number_of_songs
        self.album_artist = album_artist

albums = [Album("The Dark Side of the Moon", 10, "Pink Floyd")]
albums.index(Album("The Dark Side of the Moon", 10, "Pink Floyd"))

If we try this, we still find that the album is not in the list.


Because Python doesn’t know that these are the same album:

>>> x = Album("The Dark Side of the Moon", 10, "Pink Floyd")
>>> y = Album("The Dark Side of the Moon", 10, "Pink Floyd")
>>> x == x
>>> x == y

And that happens because of how equality is defined:


The OP also entered the wrong syntax when checking if it is the main file.

I suppose we won’t know until they actually enter it correctly per forum guidelines.

As Karl pointed out: if they hadn’t indented that block, they would have seen an IndentationError and not the problem that they are actually asking about.

You can see what the OP posted here:

The indentation looks OK, but there’s this line:

    (f"\nIndex of Dark Side of the Moon: {dark_side_index}")

It looks like it’s missing print.

Also, looking at the traceback, the coed was looking for Album(“Dark Side of the Moon”, None, None), so, when defining a __eq__ method, that use of None to mean “don’t care” needs to be taken into account.


Yeah, like I stated, I did a quick scan after seeing multiple blocks of either code or error messages. First thing to set straight is to have the OP enter the code correctly for proper analysis/inspection. I didn’t inspect the code or the error messages tbh. Would do that once the code was entered correctly.

Ok, thank you for providing this detail. Apparently there was a bad copy and paste because the line where he checks if the file name is '__main__' was entered correctly in that link but not on this post.

That’s part of what happens when code isn’t properly formatted. The double underscores, in Markdown, are an alternative to double asterisks for boldface.

using dataclasses you can create a Value Object, where the identity (x == y) is given by the values of its properties:

from dataclasses import dataclass

class Album:
    album_name: str
    number_of_songs: int
    album_artist: str

you then can have:

>>> x = Album("The Dark Side of the Moon", 10, "Pink Floyd")
>>> y = Album("The Dark Side of the Moon", 10, "Pink Floyd")
>>> x == x
>>> x == y
1 Like

I really thank you for your responses, it has indeed helped me a lot. The problem that I was having required me to included equality function inside the Album class as shown in the code below.
This function makes the program to run as it was supposed to do. Thanks team!

def eq(self, other): # Add equality method to allow comparison in list.index
if isinstance(other, Album):
return (self.album_name, self.number_of_songs, self.album_artist) == (other.album_name, other.number_of_songs, other.album_artist)
return False