454,516 Members | 1,819 Online
Need help? Post your question and get tips & solutions from a community of 454,516 IT Pros & Developers. It's quick & easy.

# Declaring variables from a list

 P: n/a Hi, If I got a list is it possible to declare a variable from the items in that list? Code Sample: Blob = ['Var1', 'Var2', 'vAR3'] i = 5 for listitems in Blob: i += 1 listitems = i print Var1 6 print Var2 7 print vAR3 8 Something like that? This doesn't work (obviously) but is there a way to do this? TIA, Cacti Jul 18 '05 #1
6 Replies

 P: n/a "Cactus" wrote: If I got a list is it possible to declare a variable from the items in that list? Code Sample: Blob = ['Var1', 'Var2', 'vAR3'] i = 5 for listitems in Blob: i += 1 listitems = i print Var1 6 print Var2 7 print vAR3 8 Something like that? This doesn't work (obviously) but is there a way to do this? why? if you want a dictionary, use a dictionary (see the tutorial for details). Jul 18 '05 #2

 P: n/a Python has a builtin function called locals which returns the local context as a dictionary locals = locals() locals["a"] = 5 a 5 locals["a"] = "changed" a 'changed' On 8 Apr 2005 13:55:39 -0700, Cactus wrote: Hi, If I got a list is it possible to declare a variable from the items in that list? Code Sample: Blob = ['Var1', 'Var2', 'vAR3'] i = 5 for listitems in Blob: i += 1 listitems = i print Var1 6 print Var2 7 print vAR3 8 Something like that? This doesn't work (obviously) but is there a way to do this? TIA, Cacti -- http://mail.python.org/mailman/listinfo/python-list -- http://blogs.applibase.net/sidharth Jul 18 '05 #3

 P: n/a On Sat, Apr 09, 2005 at 03:15:01AM +0530, Sidharth Kuruvila wrote: Python has a builtin function called locals which returns the local context as a dictionary locals = locals() locals["a"] = 5 a 5 locals["a"] = "changed" a 'changed' From Python lib reference: """ locals() ... Warning: The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter. """ Jul 18 '05 #4

 P: n/a What I gave was a bad solution. Something that works right now, but probably shouldn't be done. On Apr 9, 2005 3:37 AM, Inyeol Lee wrote: On Sat, Apr 09, 2005 at 03:15:01AM +0530, Sidharth Kuruvila wrote: Python has a builtin function called locals which returns the local context as a dictionary>> locals = locals()>> locals["a"] = 5>> a 5>> locals["a"] = "changed">> a 'changed'From Python lib reference: """ locals() ... Warning: The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter. """ -- http://mail.python.org/mailman/listinfo/python-list -- http://blogs.applibase.net/sidharth Jul 18 '05 #5

 P: n/a You can use the built-in statement exec (http://www.python.org/doc/2.4.1/ref/exec.html) : # Blob = ['Var1', 'Var2', 'vAR3'] # i = 5 # for listitems in Blob: # i += 1 # exec('%s = i' %listitems) # # print Var1, Var2, vAR3 Regards, Pierre Jul 18 '05 #6

 P: n/a "Fredrik Lundh" wrote in message news:... "Cactus" wrote: If I got a list is it possible to declare a variable from the items in that list? Code Sample: Blob = ['Var1', 'Var2', 'vAR3'] i = 5 for listitems in Blob: i += 1 listitems = i print Var1 6 print Var2 7 print vAR3 8 Something like that? This doesn't work (obviously) but is there a way to do this? why? if you want a dictionary, use a dictionary (see the tutorial for details). Thanks, I'll look in to that. Seems like that will work.... Cacti Jul 18 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion.