0

I want to create multiple variable through for loop to further use and compare in the program.

Here is the code -

for i in range(0,len(Header_list)):
  (f'len_{i} = {len(Header_list[i]) + 2 }')
  print(len_0);print(f'len{i}')    
  for company in Donar_list:
     print(company[i],f"len_{i}")
     if len(str(company[i])) > len((f"len_{i}")) :
        (f'len_{i}') = len(str(company[i]))
        print(f"len_{i}")

But what is happening, though I managed to create variable len_0,len_1,len_2... in line-2, in line - 3 I also can print len_0..etc variable by only using print(len_0), but I can't print it the values of these by - print(f'len_{i}')

In line 5 I also can't compare with the cofition with my intension. I want it do create variable and compare it further when as it necessary, in this case under for loop.What should I do now? I am a beginner and I can do it using if statement but that wouldn't be efficient, also my intention is not to create any **data structure ** for this.

I don't know whether I could manage to deliver you what I am trying to say. Whatever I just wanna create different variable using suffix and also comprare them through for loop in THIS scenario.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Unless it's critical to dynamically create variables, using a dictionary is probably what you're looking for. – Adid Apr 24 '22 at 04:43
  • Can you share the data in `Header_list`? – Zero Apr 24 '22 at 04:45
  • company_details = "Company ID, Company Name, Name Code, Total Donations Paid, Donation Status" Header_list = company_details.split(", ") – Faysal Kabir Ashik Apr 24 '22 at 05:10
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – 0x5453 Apr 27 '22 at 19:35

3 Answers3

0

Instead of dynamically creating variables, I would HIGHLY recommend checking out dictionaries.
Dictionaries allow you to store variables with an associated key, as so:

variable_dict = dict()
for i in range(0,len(Header_list)):
  variable_dict[f'len_{i}'] = {len(Header_list[i]) + 2 }
  print(len_0)
  print(f'len{i}')
  for company in Donar_list:
     print(company[i],f"len_{i}")
     if len(str(company[i])) > len(variable_dict[f"len_{i}"]) :
        variable_dict[f'len_{i}'] = len(str(company[i]))
        print(f"len_{i}")

This allows you to access the values using the same key:

len_of_4 = variable_dict['len_4']

If you REALLY REALLY need to dynamically create variables, you could use the exec function to run strings as python code. It's important to note that the exec function is not safe in python, and could be used to run any potentially malicious code:

for i in range(0,len(Header_list)):
  exec(f'len_{i} = {len(Header_list[i]) + 2 }')
  print(len_0);print(f'len{i}')    
  for company in Donar_list:
     print(company[i],f"len_{i}")
     if exec(f"len(str(company[i])) > len(len_{i})"):
        exec(f'len_{i} = len(str(company[i]))')
        print(f"len_{i}")
Adid
  • 1,504
  • 3
  • 13
0

In python everything is object so use current module as object and use it like this

import sys
module = sys.modules[__name__]
Header_list=[0,1,2,3,4]
len_ = len(Header_list)
for i in range(len_):
  setattr(module, f"len_{i}", Header_list[i]+2)

print(len_0)
print(len_1)
Deepak Tripathi
  • 3,175
  • 1
  • 8
  • 21
-1
Header_list=[0,1,2,3,4]
for i in range(0,5):
  exec(f'len_{i} = {Header_list[i] + 2 }')
  print(f'len{i}') 

output:

len0
len1
len2
len3
len4
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
ZVY545
  • 384
  • 1
  • 13
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Apr 24 '22 at 11:23
  • `exec` should almost always be avoided. In this case it looks like OP just needs a `dict`. – 0x5453 Apr 27 '22 at 19:36