Ask coding questions

← Back to all posts
Sometimes .remove fails
Vhizi (0)

so, this is a piece of code that drafts every player in the premier league (association football), in the form of 26 player teams (starting 11 and 15 players as backups). it is coded in python because that is the language i am most fluent in due to GCSE's. sometimes within the code, the ".remove" doesn't work, this has only been documented once, though so i'm wondering if it is just a thing i have to accept as an anomaly or if i have to fix something. much appreciate any help, thanks.

Vhizi (0)
InvisibleOne (2678)

.remove() has always worked for me, the only time I've every found it to not work was if it was something I did wrong.

Vhizi (0)

@InvisibleOne the thing is, the code is perfectly fine (as far as i can tell), might just be something i have to accept as an anomaly

Kai_Justice (221)

I've always seen .remove as very finicky, if it continues to not work I usually just end up replacing it with this:

itemToFind = "a thing"
for index, item in enumerate(testList):
    if item == itemToFind:
        del testList[index]

or this as a shorthand

testList = [item for item in testList if item != itemToFind]
LukeWright (114)

where exactly does .remove not work? Also, to make your code easier to read you could probably do

Vhizi (0)

@LukeWright the only recorded case i have seen is in the subroutine for burnley, it does seem to indicate randomness though, seeing as all the subroutines are almost identical. i had thought of using the code you suggested, but seeing as i need to use the data that is randomised at a later date i wasnt sure if it would work, it also might make it harder for people that have little to no skill in the language to understand seeing as this was made for a discord server.

Vhizi (0)

@Vhizi currently 10pm for me, ill answer any replies in the morning.

LukeWright (114)

I cannot see any errors in the goalkeepers section for burmley. @Vhizi

Vhizi (0)

@LukeWright that is what I thought, must just be an anomaly.