-6

I want to make variables like this:

Var1 = 0
Var2 = 0
Var3 = 0
Var4 = 0

into something like this:

allvar = (Var1 = 0, Var2 = 0, Var3 = 0, Var4 = 0)

I want a list like this because I want to use the remove() function to remove variables as the user inputs a specific thing.

dtell
  • 2,488
  • 1
  • 14
  • 29
  • 3
    Why a list? Why not a dict? – Aran-Fey Jun 27 '18 at 19:30
  • In a dictionary you can delete an item keyed by one of your vars with either "del" or "pop". https://stackoverflow.com/questions/5844672/delete-an-element-from-a-dictionary – myx Jun 27 '18 at 19:32

1 Answers1

6

Why not use a dictionary for that?

This way you could add and remove values in/from your dictionary just as you wanted.

my_dict = {
   'var1': 0,
   'var2': 0,
    ...
}

You can delete by doing my_dict.pop('var1') if you need the value back or del my_dict['var1'] otherwise and add items with my_dict['var3'] = 0.

JeyzerMC
  • 141
  • 5
  • 1
    Your code for the deletion of an element is too complicated. `del my_dict['var1']` is a lot clearer and with less overhead. – Matthias Jun 27 '18 at 20:11
  • pop is useful if you need the value back, but I added the `del` way too for the other use cases, thank you! – JeyzerMC Jun 27 '18 at 20:45