-1

Let's say I have three variables:

var_1 = 'blank.png'
var_2 = 'blank.png'
var_3 = 'blank.png'

I want to change those variables to some values (file paths) I have in a list:

paths = ['dir/file_1.png', 'dir/file_2.png', 'dir/file_1.png']

The problem is that the paths list changes based on user input, there could be 1 entry, or 2, is there a way to make a loop which for every index value in the list, sets the variable value accordingly, something like:

for i in paths:
    var_1 = i

With an increment for the variable?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
RingK
  • 83
  • 12
  • I'd like to know why I have a "-1" for this question... If it's a stupid question I'd like to know why, to avoid the same mistake another time... – RingK Feb 12 '16 at 21:50
  • It's not very clear what you are asking for here. Try to be specific about what you have, what you want, and what you've tried that didn't work. – Chad S. Feb 12 '16 at 22:10
  • @ChadS. "It's not very clear what you are asking for here." I have 3 variables and I need to change their content based on the list entries which changes with user input. "Try to be specific about what you have" I have 3 variables and a list that contains user generated file paths. "what you've tried that didn't work." I tried making a for loop to achieve this but I can't find a way. – RingK Feb 12 '16 at 22:57
  • Just use the list as is. – Karl Knechtel Jul 05 '22 at 00:55

1 Answers1

0

If there isn't a reason that you have to name your variables individually, you can just keep the values in a list:

vars = ['blank.png'] * 3;

You can then just assign them like this:

vars[:len(paths)] = paths[:len(paths)]

If there are more paths then vars, vars will grow. In your code you just have to replace var_n with vars[n-1]

Thomas
  • 4,980
  • 2
  • 15
  • 30
  • Thanks for the help, but I need my variables to have a specific name. What I need is to change their content based on the list entries which changes with user input. – RingK Feb 12 '16 at 22:55
  • Then you are looking for something like [how-to-get-the-value-of-a-variable-given-its-name-in-a-string](http://stackoverflow.com/questions/9437726/how-to-get-the-value-of-a-variable-given-its-name-in-a-string) and the answer depends on where your variables are located. – Thomas Feb 12 '16 at 23:31
  • Thanks! I solved my problem doing as stated in the question you linked! – RingK Feb 12 '16 at 23:45