Page 215 -
P. 215
custom data objects
Tonight’s talk: To use a list or not to use a list?
Dictionary: List:
Hi there, List. I hear you’re great, but not always
the best option for complex data. That’s where I
come in. What?!? Haven’t you heard? You can put anything
into a list, anything at all.
True. But when you do, you lose any structure
associated with the data you are processing. Well…assuming, of course, that structure is
important to you.
Isn’t it always?
Ummm, uh…I guess so.
You guess so? When it comes to modeling your data
in code, it’s best not to guess. Be firm. Be strong. Be
assertive. Use a dictionary. That sounds like a slogan from one of those awful
self-help conferences. Is that where you heard it?
[laughs] Oh, I do love your humor, List, even when
you know you’re on thin ice. Look, the rule is
simple: if your data has structure, use a dictionary, not a
list. How hard is that? Not that hard, really. Unless, of course, you are a
list, and you miss being used for every piece of data
in a program…
Which rarely makes sense. Knowing when to use a
list and when to use a dictionary is what separates
the good programmers from the great ones, right?
I guess so. Man, I do hate it when you’re right!
Geek Bits
The Python dictionary is known by different names in other programming languages. If you hear other
programmers talking about a “mapping,” a “hash,” or an “associative array,” they are talking about a “dictionary.”
you are here 4 179