0

I'm trying to work out how to split a list and create variables with those parts, something like the below:

a_list = ["var1", "var2", "var3"]

for x in range(len(a_list)):
    a_list.pop()

I'd like the variables created to be named as they are in a_list and I won't know how many components are in a_list or what they are named.

Is this possible? I've read quite a few similar questions on here but they don't come with all the requirements mine does.

Callum
  • 195
  • 2
  • 22
  • 3
    They probably told you not to do this and that you need a dictionary, too. Don't do this. You want a dictionary. – roganjosh Nov 24 '19 at 20:38
  • 2
    Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – bad_coder Nov 24 '19 at 20:42
  • Agree with roganjosh, probably you've looked already here: https://stackoverflow.com/questions/5036700/how-can-you-dynamically-create-variables-via-a-while-loop. I have similar issue, being expert PHP developer when learning python I found some missing stuff, for example something like `$$variable_name = 'value'`, but it is what it is and each language has its own coding standards, so the best is to do what experts suggest – Anatoliy R Nov 24 '19 at 20:51
  • @AnatoliyR I can't profess to know how this works in PHP but I also can't imagine that having an unlimited (and unknown) number of names just floating around in any language is useful. You'd surely always want a defined collection that you can iterate? – roganjosh Nov 24 '19 at 20:54
  • @roganjosh PHP is just an example, but personally I try to avoid using that - for another reason: it's hard to search all instances of a particular variable. However, you want it or not, developers do it and sometimes it makes sense. This is really about language philosophy and coding style. – Anatoliy R Nov 24 '19 at 21:04
  • Ok, happy to use a dictionary instead but how would I go about it if ```a_list``` was a dictionary, ```a_dict``` and the values of the variables were all set to ```0```? – Callum Nov 24 '19 at 21:22

1 Answers1

0

If there are a fixed number of parts, you can do this with a destructuring assignment:

>>> a_list = ["var1", "var2", "var3"]
>>> v1, v2, v3 = a_list
>>> v1
'var1'
>>> v2
'var2'
>>> v3
'var3'

If there aren't a fixed number of parts, then you don't know how many variables you would be declaring and hence you wouldn't know in later code which variables were declared or not. In that case, it's more useful to leave the values in the list so they can be accessed by index.

kaya3
  • 47,440
  • 4
  • 68
  • 97
  • I'm hoping not to have to set the names of the variables but rather use them from the list itself – Callum Nov 24 '19 at 21:23
  • There is no such thing as a variable without a name. If you want to use them from the list itself, then... you already have them in a list, so what is the problem? – kaya3 Nov 24 '19 at 21:29
  • I want to use the components of the list as the variables, rather than hardcode thr variable names. I need them out of a list so I can assign/update their values from the result of a function ```var1,var2,var3 = a(int, int) – Callum Nov 24 '19 at 22:31
  • You want to use them from the list itself, but you need them out of the list to do that? I don't follow you at all. Are you aware that a function can modify the contents of the list without needing to return anything? – kaya3 Nov 24 '19 at 22:32
  • So I need (I think) to get the variables into a structure as follows ```var1,var2,var3 = a(int,int)``` so that the numbers that are returned from ```a()``` are assigned to the three variables. Note that I'm using three variables in this example but it could be a changing number variables – Callum Nov 24 '19 at 22:42
  • Before you ever utter the words "a changing number of variables", stop, and don't try to make anything like that work. Whatever problem you are actually trying to solve, you need to figure out how to do it by getting and setting the values in the list. – kaya3 Nov 24 '19 at 22:46
  • Ok, I'll go back to the drawing board and try to go down the dictionary route as suggested – Callum Nov 24 '19 at 22:51