473,667 Members | 2,650 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 1639
"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********@sii mage.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*****@python ware.com> wrote in message news:<ma******* *************** *************** *@python.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
6326
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 them i can't define (hard coded) a fixed length because the application is reading data from another file (In that file they have the same lenght). Is it possible to use the lenght i obtain from reading that other file to declare my variable or...
2
2132
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 efficient than repeatedly referencing the member in a loop? Maybe using a string isn't the best example, but hopefully you get the idea! * example (referencing member):
6
1586
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
2423
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 program which control to requery. This makes it easy to requery controls on a different form. However, up until today, I was dimming a new local variable for each control I wanted to deal with ( E.G. Dim cbxctl as control) and then setting that...
3
2258
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() on WebForm1.aspx, I use "Public Property" and I declare variable _oRpte as Friend Shared. That's my problem. If I don't declare _oRpte as Friend Shared, I can't use WebForm1.oRpte() on other webpage. If I declare _oRpte as Friend Shared, I can use...
2
2347
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
7505
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, like this: for(var i=0; i<list; i++) { var name = list; }
6
2980
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
6570
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
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8889
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8650
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.