0

sorry for the confusing title, I am unsure of what I am asking but I should be able to explain it better.

I have a list of items

list = ['s1',s2','s3']

I am iterating of the list no worries:

for l in list:
    print l

My old code which I am looking to upgrade now is hard coded:

s1.write(dict(x=r[0], y=r[1]))
s2.write(dict(x=r[0], y=r[1]))
s3.write(dict(x=r[0], y=r[1]))

But I want to be able to use the aforementioned iterator to be the prefix of each of these three lines, you will notice they are hardcoded with s1.write, s2.write etc. I want these lines to be created on each iteration, rather than being static.

I know I am not explaining using correct terminology, i apologise for the confusion.

Any ideas if I can run these three lines individually, with the slight change being made on each loop?

What I envisage is something like:

for li in list:
    li.write(dict(x=r[0], y=r[1]))
Benno
  • 109
  • 1
  • 16

1 Answers1

2

Rather than the names of the variables, why don't you just store the variables themselves in a list?

l = [s1, s2, s3]

Then you can call this method off of each variable

for s in l:
    s.write(dict(x=r[0], y=r[1]))
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218