I have a list of objects. The list contains Players, each Player has 3 attributes. player_name, player_number, and player position. I want to search through the list by player_name, find the first instance of that object and remove the object.
p1 = Player("Pete", "12", "runner")
p2 = Player("John", "5", "catcher")
p3 = Player("John", "29", "guard")
player.list = [p1, p2, p3]
I want to remove the first instance of john, which would be p2
I can search through the list and find him but im lost on how to remove the whole object of him. I want to use .remove() somehow since i know it works on the first time it sees it but i cant figure it out.
for i in player.list:
if i.player_name == "John":
player.list.remove(i)
The issue with my approach is its removing all of them because its looping through the whole list. what would be a better way to search this list, maybe add a counter or something?