Item in list in dictionary Python

I am creating a battleship game, I have this looking dictionary with ships and coordinates as their values and they are all saved into a self.__dict dictionary object in a main class.

{'battleship': ['A1', 'A2', 'A3', 'A4'], 'cruiser': ['E1', 'F1', 'G3'], 'destroyer': ['D5', 'D6'], 'submarine': ['I9']}

my code should place on the board “*” when the entry not in the ships coordinates and “X” when its in coordinates.

for some reason the code not reading the shot that it’s in the values and prints the miss “*” shot instead.

changin_line variable, is the new line inserted in the game board with the mark included.

x_index variable, is the picked coordinates that referrers where the mark should be placed.

shot = "A3"
        
if shot not in self.__dict.values():
     changin_line.insert(x_index, "*")   
else:
     changin_line.insert(x_index, "X")

Answer

self.__dict.values() is a list of lists, not a single list of positions. So you’re comparing a string to a list, and this will never be true.

You need to drill down another level:

if not any(shot in ship_pos for ship_pos in self.__dict.values()):
    changin_line.insert(x_index, "*")
else:
    changin_line.insert(x_index, "X")

It might be better to invert your data structure. Instead of using the ship types as keys, use the coordinates as keys:

{'A1': 'battleship', 'A2': 'battleship', 'A3': 'battleship', 'A4': 'battleship',
 'E1': 'cruiser', 'F1': 'cruiser', 'G1': 'cruiser',
 ...
}

Then you can test if shot in self.__dict:. And if you want to know which ship was hit, it’s simply self.__dict[shot].

BTW, __dict seems like a poor name for this variable. Call it something like ship_positions.