Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove only one instance from a list in Python?

Tags:

python

list

So I have a Python script that reads in a .csv file of a play-by-play of a basketball game to calculate a plus/minus by lineup. It keeps track of the current players on the floor in a list of the player's last names, and I encounter an error when a team has two players with the same last name (the play-by-play data doesn't use first names, only numbers and last names, and I don't keep track of their numbers). The problem comes when I use lineup.remove(player) on one of the duplicate names, it removes both from the list. Is there an easy way to only remove one, and if so, can I specify which?

Example list would be

['JONES', 'PATTERSON', 'SMITH', 'JONES', 'WILLIAMS']
like image 418
John Avatar asked Mar 13 '23 07:03

John


1 Answers

From the docs:

list.remove(x) Remove the first item from the list whose value is x. It is an error if there is no such item.

lineup = ['JONES', 'PATTERSON', 'SMITH', 'JONES', 'WILLIAMS']
lineup.remove('JONES') #should just remove the first occurrence of JONES
like image 186
Garrett R Avatar answered Mar 15 '23 22:03

Garrett R