list vs. dict
eppstein at ics.uci.edu
Thu Feb 28 22:10:25 CET 2002
In article <3C7E8D47.21BE30B3 at ccvcorp.com>,
Jeff Shannon <jeff at ccvcorp.com> wrote:
> No, it's not something that people often use. Generally, if you want
> dict-like behavior, you just use a dict. The times that it's worth the
> effort to fake the behavior with something else, are pretty unusual.
> The speed difference between lists and dicts isn't all that significant,
> anyhow, and which one has the advantage depends on how you're using it.
> Python dicts are pretty amazingly nifty, really; there's no reason to be
> shy about using them.
I agree. I had been looking at some code where I needed two-dimensional
arrays, which I was coding as lists of lists, and it was getting pretty
ugly initializing the list of lists so that each list is distinct --
[[None]*x]*y doesn't work for reasons I'll leave as an exercise.
Finally, I realized I could just use a dict. No tricky initialization, and
the syntax for double indexing dict[x,y] is even slightly nicer than
list_of_lists[x][y]. Unlike a list of lists, it's easy to grow the range
of indices: just start using them. It would be good if dict.has_key(x,y)
worked without having to double the parentheses, but that's only a minor
David Eppstein UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/
More information about the Python-list