0

I am trying to create dynamic variable names as part of a loop and assign then appropriate values. To demonstrate more clearly what I am after I have created this demonstration code:

var1 = ["one","two","three"]
var2 = ["red","blue","green"]

for i in 0 to 2:
    var3 = var1[i] #cycle through first list
    var4 = var2[i] #cycle through second list

    var5 = var3,var4 #make the name of var5 a concatenation of var 3 and var 4

    var5 = 1 #assign this dynamic variable name an arbitary value

print var5

The bit that I am just not sure how to approach is the line var5 = var3,var4. I'm not even sure that this is the best way to approach the problem. Could someone please:

1) Fill in the blank the line referenced above 2) Suggest an alternative syntax altogether

The reason I need dynamic variable names is in my real code I am using for loops where each number in the loop has different conditions attached to it. Apologies if I am a little off beam with my methodology.

EDIT:

The responses I have had suggest I have not articulated what I require clearly. My sample output needs to be of the format:

one_red = 1
two_blue = 1
three_green = 1

The reason why I require these to have different variable names is that I will eventually be joining my outputs together. So my final output would be:

111

The joining section is unimportant for now, but what is important is that I am programatically creating variables in a loop that will each be assigned their own value.

Thanks

gdogg371
  • 3,879
  • 14
  • 63
  • 107
  • 4
    Dynamically assigning variables like this is almost never what you want. Use a list or dictionary instead (whichever is more appropriate). – Colonel Thirty Two Nov 10 '14 at 18:24
  • 2
    Instead of saving to dynamic variable names, use a dictionary. See [keep data out of your variable names](http://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html) – David Robinson Nov 10 '14 at 18:24
  • Your edited question does not give any reason at all why these values have to be in separate variables. Joining these values is much easier when they are stored in the same data structure than if they are stored all over your local namespace. If you explain how you are joining these values, I can demonstrate why it is easier with a dictionary. (Please see the link in my comment above, and also [Why you don't want to dynamically create variables](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html)). – David Robinson Nov 10 '14 at 18:40

3 Answers3

2

Use a dictionary instead of dynamically assigning variable names. Also, you can shorten the var3 and var4 lines using zip- see Is there a better way to iterate over two lists, getting one element from each list for each iteration?.

At that point, your code will look something like:

var1 = ["one","two","three"]
var2 = ["red","blue","green"]

d = {}
for var3, var4 in zip(var1, var2):
     d[var3 + var4] = 1

print d
# {'twoblue': 1, 'threegreen': 1, 'onered': 1}
Community
  • 1
  • 1
David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • Hi, thanks for responding. Please see my amended question, as I don't think I was being very clear about what I was asking for. – gdogg371 Nov 10 '14 at 18:37
0

I would use a dictionary or something else, rather than trying to create variables by name. To concatenate from each list:

>>> names = [''.join(i) for i in zip(var1, var2)]
>>> names
['onered', 'twoblue', 'threegreen']

Then you can make your dictionary to look things up, for example

>>> {name : index for index, name in enumerate(names)}
{'twoblue': 1, 'onered': 0, 'threegreen': 2}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Hi, thanks for responding. Please see my amended question, as I don't think I was being very clear about what I was asking for. – gdogg371 Nov 10 '14 at 18:38
0

var5 = var3,var4 #make the name of var5 a concatenation of var 3 and var 4

Be careful that this line assigns to var5 the value tuple(var3, var4). It doesn't concatenate the two strings.

As David Robinson and others pointed out, use a dictionary. I just thought I'd flag the concatenation issue.

  • Hi, thanks for responding. Please see my amended question, as I don't think I was being very clear about what I was asking for. – gdogg371 Nov 10 '14 at 18:37