By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,362 Members | 1,791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,362 IT Pros & Developers. It's quick & easy.

list/tuple/dict question

P: n/a
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!!)

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', [] ]

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

thoughts/comments/code samples would be reatly appreciated.

looking at various sites/samples cia google hasn't helped...

thanks
Aug 17 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
Aug 18 '08 #2

P: n/a
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...@websiteburo.invalidwrote:
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 liststhat
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.
Aug 18 '08 #3

P: n/a
En Mon, 18 Aug 2008 11:02:20 -0300, gundlach <gu******@gmail.comescribió:
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.
>From http://docs.python.org/lib/built-in-funcs.html#l2h-47
locals(): Update and return a dictionary representing the current local symbol table. Warning: The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter.

So modifying locals() is unsafe at least. Do as everyone else suggested and use a dictionary. If using ns['cat'] really annoys you so much, define a generic attribute container:

class NS(object): pass

ns = NS()
ns.cat = 1
ns.dog = 2
ns.zoo = ns.cat + ns.dog

--
Gabriel Genellina

Aug 19 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.