list/tuple/dict question

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Mon Aug 18 03:32:59 EDT 2008


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