vendredi 21 février 2020

.remove function not working with an 'if' check in lists

I'm fairly new to programming and python itself- I'm trying to use the .remove function to delete an item from a list if it exists in that list (as to not get the nameError).

q = ["cat","dog","fish","hamster","horse"]

#Request element name to delete from queue
removeElement = input("Please type in the element name to remove from the queue: ")
#Remove the given element from the list
q.remove(removeElement) if 'removeElement' in q else None

print(q)

Unfortunately, if I try and use the 'if' checker it the item isn't removed from my list- why is this and how can I fix this issue?

Aucun commentaire:

Enregistrer un commentaire