Reputation: 15
I'm attempting to make a simple To Do List in Python, and I'm struggling with the Remove Item function I have made. See, I want to make it so you can remove an item, and bring it back if needed. I'm currently using the pop function, but the pop function can't remove items by value. Is there anyway to see if an index of a list is equal to a string? Is there a better function than pop to do the thing I'm attempting?
# Example Code:
items = ["Bananas", "Apples", "Mangos", "Avocados"]
print(items)
removeItem = input("What item would you like to remove? ")
poppedItem = items.pop(removeItem)
print(items)
items.append(poppedItem)
print(items)
I tried everything I could to fix the problem, such as simply deleting the item with the delete function to attempting to compare the index value to the index number, but it's either not working properly or I just couldn't get it to work. I don't know any other way to delete an item and then bring it back later. Please help.
Upvotes: -1
Views: 59
Reputation: 1
You can use method s.remove(x)
. It removes the first item from s
where s[i]
is equal to x
. Also s.remove(x)
raises ValueError
when x
is not found in s
. You can see all the list methods here: https://docs.python.org/3/tutorial/datastructures.html.
You can bring it back if you replace poppedItem
for removeItem
in line items.append(poppedItem)
. New code:
items = ["Bananas", "Apples", "Mangos", "Avocados"]
print(items)
removeItem = input("What item would you like to remove? ")
items.remove(removeItem)
print(items)
items.append(removeItem)
print(items)
Upvotes: 0