473,509 Members | 3,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring variables from a list

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 1629
"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).

</F>

Jul 18 '05 #2
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 <le*******@msn.com> 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
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
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 <in********@siimage.com> 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
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
"Fredrik Lundh" <fr*****@pythonware.com> wrote in message news:<ma**************************************@pyt hon.org>...
"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).

</F>


Thanks,

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

Cacti
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
6309
by: Stefanie | last post by:
Hi, I have a question about declaring variables for writing data to a file for random access. I have two string variables declared in a module (user-defined types). The point is that for one of...
2
2123
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more...
6
1581
by: Joe | last post by:
Is it generally considered "better" (i.e. clearer) to declare all variables at the start of a function or as you need them within it? Joe
1
2413
by: ColinWard | last post by:
Hi guys. I have a question about declaring variables. I do a lot of re-querying of controls in my database and I use the Set statement with a variable set to the name of the control to tell the...
3
2246
by: jbeteta | last post by:
Hello, I have a problem declaring variables. I need to create an object oRpte as ReportClass on WebForm1.aspx and be able to use its value on WebForm2.aspx. For declaring the property oRpte()...
2
2341
by: vlsidesign | last post by:
Here is my a portion of my program: #include <stdio.h> main() { int fahr, celsius; int lower, upper, step; int fc_conv(int fahr); ... snip ... }
8
7496
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
6
2969
by: =?Utf-8?B?QUw=?= | last post by:
Hi I usually stick to the convention of not declaring variables in my bodies of "loops" (including foreach) ie int x; for (int i = 0; i < 10; i++) {
10
6553
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7347
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7416
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7073
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.