Declaring variables from a list

C

Cactus

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
 
F

Fredrik Lundh

Cactus said:
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).

</F>
 
S

Sidharth Kuruvila

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'


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
 
I

Inyeol Lee

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.
"""
 
S

Sidharth Kuruvila

What I gave was a bad solution. Something that works right now, but
probably shouldn't be done.

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.
"""
 
C

Cactus

Fredrik Lundh said:
Cactus said:
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).

</F>

Thanks,

I'll look in to that. Seems like that will work....

Cacti
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top