Search code examples
pythonfor-loopvariablespyqt5qt-designer

Define multiple variables using for loop on python


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?


Solution

  • 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.