0

I am a beginner in python coding. I have a couple of questions:

Question 1: How Can I define multiple variables using for-loop in Python

global I1_1
global I1_2
global I1_3
global I1_4
global I1_5
global I1_6

Questions 2: How can I call multiple objects using the for-loop.

For example, I have these objects:

lineEdit_1
lineEdit_2
...
lineEdit_100

I would like to call everyone of them in a for loop. I tried to look for something similar online but I didnt find. My first thought was to define a range for a variable i (1-100) then call lineEdit_i. Any idea?

jalazbe
  • 1,801
  • 3
  • 19
  • 40
  • This post might help you. https://stackoverflow.com/questions/41645898/using-a-loop-to-create-and-assign-multiple-variables-python – jalazbe Nov 27 '18 at 15:23
  • Don't do that. Put all of this variables in a list or an array instead –  Nov 27 '18 at 15:25
  • Use `global()['varname'] = value` in a loop. The previous question *is not* clear on this is possible. Adding answer to that - also, it's not exactly clear that the two question are asking the same thing, it seem to me the previous one is broader and this is a more specific case. – Rocky Li Nov 27 '18 at 15:31

1 Answers1

0

It sounds like you're talking about dynamically creating variables, which to my understanding you cannot do. What you can do, however, is leverage the python data structure of lists. Initialize a list by setting a variable like my_list = [] then you can .append() any of your variables to the list, use a for-loop to loop through all your variables, and do whatever you need to in your loop.