list/tuple/dict question

B

bruce

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
 
B

Bruno Desthuilliers

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

gundlach

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


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

Gabriel Genellina

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.

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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top