set variable to looping index?

Jan Kaliszewski zuo at
Wed Jul 29 18:57:02 EDT 2009

30-07-2009 <mdekauwe at> wrote:

> All I was trying to do was call a function and return the result to an
> a variable. I could admittedly of just done...
> var1 = some_function(var1_fn)
> var2 = some_function(var2_fn)
> var3 = some_function(var3_fn)
> where var1_fn, var2_fn, var3_fn are just filenames, e.g. var1_fn =
> 'x.txt'. But I figured I would try and make it slightly more generic
> whilst I was at it, hence my attempt to use the filenames to create
> the variable names (hence the loop).


Then you could also consider using simply lists:

filenames = p'foo', 'bar', baz']
results = []
for name in filenames:

If filenames were generated according to a particular pattern, you can
mimic that pattern and generate filenames list using
list-comprehension, e.g.:

filenames = ['file{nr}.txt'.format(nr=nr) for nr in range(13)]



Jan Kaliszewski (zuo) <zuo at>

More information about the Python-list mailing list