1

I'm trying to make a program which you can add stuff to your shopping list.

shopping_list = ["Eggs", "Milk", "Bread"]

Now i know how to add and remove items, but how do i delete the string or number in a variable without losing the variable?

price = "£12"

If I do del price I destroy the variable. If I try print price i get an error:

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    print price
NameError: name 'price' is not defined
3
  • 2
    You seem to have a mistaken mental model of how Python variables work. What do you think it would mean to delete a string? What would print price do after your desired operation? Commented Jun 8, 2016 at 18:01
  • 1
    Not sure if this is what you're hoping to do, but maybe just set price to an empty string: price = "" Commented Jun 8, 2016 at 18:02
  • Why didn't i think of that? Commented Jun 8, 2016 at 18:38

1 Answer 1

4

Just do

price = None

Anything referenced by that variable will get garbage-collected.

Sign up to request clarification or add additional context in comments.

1 Comment

If there's no other reference to that object, it'll get garbage-collected.

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.