create/access dynamic growth list
Kent Johnson
kent37 at tds.net
Sat Mar 19 06:53:37 EST 2005
sam wrote:
> Since each line of the configuration file need to be parsed into "key"
> and "value" pairs, these pair of tokens need to be stored in another
> list. Can I use the following operation to store these pair of tokens?
> eg.
> Assumed macro_hash["key"]="some key"
> macro_hash["value"]="some value"
> then build a list that contains a list of macro_hash:
> macro_list.append(macro)
> when treverse the macro_list, it would be similar to perl:
> foreach macro (in macro_list)
> key = macro["key"]
> value = macro["value"]
>
Why not just store the key, value pairs as actual keys and values of a map? Creating a list of maps
seems a bit contorted here. Instead of
> macro_hash["key"]="some key"
> macro_hash["value"]="some value"
followed by creating a list of macro_hashes, just use
macro_hash["some key"] = "some value"
You lose the order of values but gain much easier lookup.
Alternatively just create a list of (key, value) pairs with
macro_list = []
macro_list.append( ("some key", "some value") )
which preserves the order at the expense of easy random access.
Kent
More information about the Python-list
mailing list