1

I am making a text-based adventure game and am having a little trouble.

I want to delete an item from an inventory list however, it's index is unknown as the user may of picked up other items before the one i'm trying to delete.

How would I go about deleting it?

the list is:

inventory = ["sword", "healing potion"]

and when you go through the game you pick up items and it gets added to this list.

2 Answers 2

4

You can simply use the remove method:

inventory.remove('sword')
Sign up to request clarification or add additional context in comments.

Comments

0

Above answer is the best one. Another way to do it is(maybe this could be useful too):

index = inventory.index("sword") #in this way you get the index del(inventory[index]) #and now remove it

1 Comment

or inventory.pop(index)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.