list/tuple/dict question

gundlach gundlach at gmail.com
Mon Aug 18 16:02:20 CEST 2008


Hi Bruce,

I think I get what you're asking for -- you want to actually end up
with a local variable 'cat' which points to an empty list, so that you
can then do

cat.append('foot')

or whatever.

The problem with the last line of this code (based on your attempt):

foo=[]
foo.append('cat')
foo[0] = []

is that Python doesn't evaluate the left hand side of an assignment to
figure out what variable name you want.  foo[0] doesn't get replaced
with 'cat' as the variable name.  Instead, Python sees this line of
code as a request to store an empty list as the 0th item in the foo
array.

In C or C++, what you want to do is impossible.  However, in Python,
there's a way to specify the name of a local variable at runtime:

locals()['cat'] = []

locals() is a function call that returns a dictionary mapping all
local variable names to their values.  Just like "foo[0] = []" above
will store an empty list into the 0th item in foo, "locals()['cat'] =
[]" will store an empty list in the 'cat' entry in the locals
dictionary.

Once you've done that, you can then refer to the variable cat as
usual:

cat.append('foot')

etc.

Note that you can also *read* the value of a local variable using the
locals dictionary:

locals()['cat']

will return a list.  Trying to access an entry in the locals
dictionary that isn't defined will raise an exception, much like if
your code tried to access a variable that didn't exist.

And finally, you can also create a global variable -- one that, even
though it's defined from within a function, will be accessible
anywhere in the module -- by using the globals() dictionary instead.

Hope this helps, and good luck --
Michael


On Aug 18, 3:32 am, Bruno Desthuilliers <bruno.
42.desthuilli... at websiteburo.invalid> wrote:
> bruce a écrit :
>
> > hi guys/gals...
>
> > got a basic question that i can't get my hands around.
>
> > i'm trying to programatically create/use a list/tuple (or whatever the right
> > phrase in pyton is!!)
>
> > basically, something like:
> >  foo = []
> >  foo.append('cat')
> >  foo.append('dog')
>
> >  foo[1] = [] (and in this case, i really want to have a list called 'cat' to
> > be created!!)
>
> This doesn't "create a list called 'cat', it replaces the second element
>   (remember, sequences are zero-based) of list 'foo' with an empty list.
>
> > when i've tried this, i don't get a list called 'cat', instead (as i
> > expected) the foo[1] is now a [] (list))
>
> > so foo is now
> >  ['cat', [] ]
>
> Indeed.
>
> > ultimatelly , i want to be able to dynamically create a number of lists that
> > i name/create/manipulate on the fly, within the test app.
>
> > ie, be able to then create a list/array cat = ['a','b','c',....]
>
> > a dict doesn't seem to work, as it is essentially a series of key/values,
> > which isn't exactly what i want...
>
> Why do you think it's not what you want ?
>
> lists = dict()
> lists['cat'] = []
> lists['cat'].extend(['a', 'b', 'c', 'd'])
>
> > thoughts/comments/code samples would be reatly appreciated.
>
> Please provide more informations about your use case.




More information about the Python-list mailing list