-2

I'm trying to name variables using elements of a list.

I have 3 companies named absoft, dataco, microm.

In my script I have some methods defined for those like this one:

copyfile(absoft_old_file, absoft_copy)
copyfile(dataco_old_file, dataco_copy)
copyfile(microm_old_file, microm_copy)

Is it possible to loop those names from a list instead? It's very simple for strings, but for variable names I didn't figure out how to do that yet.

company_list = [absoft, dataco, microm]

for company in company_list:
copyfile(company + _old_file, company + _copy)

Please help

  • Such questions should always explain why they're among the estimated 0.000000000001% of cases where that's indeed a better idea than doing it the normal way. – Kelly Bundy Mar 22 '20 at 13:58
  • 1
    Does this answer your question? [How to get the value of a variable given its name in a string?](https://stackoverflow.com/questions/9437726/how-to-get-the-value-of-a-variable-given-its-name-in-a-string) – azro Mar 22 '20 at 13:59
  • `for company in ['absoft', 'dataco', 'microm']: copyfile(globals()[f'{company}_old_file'], globals()[f'{company}_copy'])` – azro Mar 22 '20 at 14:03
  • Please don't suggest things like this. Effectively nobody has a good reason to do this, and everybody who asks is better served by an explanation of why than by possible approaches. – Karl Knechtel Mar 22 '20 at 14:23
  • Good point with the "why". Here I have a short list of 3 companies but want to create a fully automated solution - say I want to add another 100 companies to those 3 in the future, how do I make this code as scalable as possible so that all I have to do is add those future companies to the company_list? – currentlyunknown Mar 22 '20 at 15:44

1 Answers1

0

Is it possible to loop those names from a list instead?

You do not want or need to do this in order to solve your problem.

You already know how to make lists.

Put things into your lists that actually let you solve the problem.

You have values with which you want to call the same function (copyfile) repeatedly. So those are the values that go into your list.

For example, you could have something like:

source_files = [absoft_old_file, dataco_old_file, microm_old_file]
dest_files = [absoft_copy, dataco_copy, microm_copy]

for source, dest in zip(source_files, dest_files):
    copyfile(source, dest)

If it seems like a bunch of extra work for no benefit, the problem is creating all those separately named variables in the first place; the values should go into such lists directly. To give a specific answer would require more detail about your overall program.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • I want to make this solution automated and scalable for another companies I would add to the list in the future. Right now I have 3 companies and might want to add another 100 - I just want to add them at the beginning of the script to the list and let the code figure out the rest. Adding "anothercompany_old_file" for each would take too much time in the future. – currentlyunknown Mar 22 '20 at 15:40
  • Like I said, don't make variables for them - just put them into the list right away. – Karl Knechtel Mar 22 '20 at 22:20