0

I'm trying to see if there's a way I can see if the list got deleted after doing del command on the list

  del users
  if bool(users):
      print("exist")

One of the ways that I tried is mentioned above. But for each of these, I get an error-

UnboundLocalError: local variable 'users' referenced before assignment

Since the list is not present, it is unable to reference it but I wanted to know if there's a way(an in-built method) I can check if it got deleted( not empty)

Shivam
  • 53
  • 1
  • 7
  • 1
    Does this answer your question? [How do I check if a variable exists?](https://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists) – Rehan Rajput Jun 19 '22 at 20:32
  • 1
    Why do you need to test that the variable is inexistent after deleting it? Any reason to believe this shouldn't be the case? – mozway Jun 19 '22 at 20:33

1 Answers1

0

You can use a try-except statement to catch the error:

    test = [1, 2, 3]
    del test
    try:
        test
        print('Test exists!')
    except NameError:
        print("It's deleted!")
TLeo
  • 76
  • 6
  • Okay, so this is an error-handling thing. I was looking for a method, if there was one, using which this could be done. But thanks for that. I think this is good. – Shivam Jun 19 '22 at 21:54