I doubt it, but lookup by key in a dictionary is likely more efficient than in such a list.
I hate it when people trot out efficiency as the prime desiderata. The reason we use more complex data types is to make programs easier to read, reason about, and modify. Efficiency comes second to correctness, and correctness follows from readability and testing. (Metadata such as types can only catch certain kinds of problems.)
That’s true, but they’re very good at solving those problems. (Or, more accurately, there are a wide class of problems that types are extremely good at solving)
I strongly agree with Derleth’s main point, though. If you use an abstract dictionary as opposed to a manifest list, then you cleanly separate the implementation details from the actually relevant behavior. This both makes the code using that dictionary easier to understand (being less cluttered with irrelevant low-level mucking about) and leaves you free to swap out the actual back-end implementation as you please.
All this is true, and I agree that it’s more important to have readable code than efficient code (except in real-time systems or similar domains, where efficiency and readability are equally important). It’s been my experience, however, that no one really appreciates the importance of maintainability until they’ve been programming for a while. Efficiency, on the other hand, is something very concrete that almost every programmer appreciates, so I tend to cite it when I can depending on my audience. YMMV.
ultrafilter: OK, that makes sense. You might also emphasize that compilers are smart and high-level types and functions allow the compiler to infer things low-level types and functions don’t, leading to better-optimized code. (As a trivial example, loops created with control structures can typically be unrolled whereas loops created with goto typically aren’t.)
Right. If you wanted to search the values, you could do: if name in bestiary.values(). However, in a large dictionary, searching the values this way will be slow. (It’s a linear search.)
As ultrafilter said, although you could fake it with a list, it would be much less efficient. A Python dictionary guarantees that all of its keys are unique, and provides quick indexing (order log(N)) on those keys. A list doesn’t have those fancy features.
Incidentally, if you want to track a bunch of unique strings (or other objects), but don’t particularly want them mapped to anything as dictionaries always do, take a look at the set data type. In recent versions of Python, it’s a built-in. In earlier versions (pre 2.4), you have to import the Set type from the sets module.