Newbie question

Alexander Stante mrpac at gmx.de
Tue Nov 2 03:46:20 EST 2004


On Mon, 01 Nov 2004 23:30:17 -0800, Uday wrote:

> My problem is that I need to dynamically generate a unique 
> variable within a for loop in Python. The name of the variable
> should be appended by the index of the loop. 
I think exec is what you want:

for x in range(10):
    exec "var_%d = %d" % (x, x)

This creates var_0 ... var_9 and assigns them the value of the index. exec
executes strings which can be dynamically created at runtime. Okay, I
guess performance isn't very well but if it is not crucial, who cares?

> I am not in python.users mailing list, please reply to me directly.
No, if you want answers you should read it, at last the thread you
started. People answering your posting are not answering because of you,
but because of all the other peoples who have the same/similar question.

Regards
Alex



More information about the Python-list mailing list