Cleaning up conditionals

Chris Angelico rosuav at gmail.com
Tue Jan 3 03:14:10 EST 2017


On Tue, Jan 3, 2017 at 6:35 PM, Deborah Swanson
<python at deborahswanson.net> wrote:
> I think I'm right that core python sequences can't be indexed in any
> fashion by strings, because strings aren't iterable. I suppose it might
> be possible for strings to be iterable in some sort of ascii char code
> order, but that seems like it could get real whacky very fast, and not
> terribly useful.

It's not because they're not iterable, but because there's a
fundamental difference between a key-value pair (dict) and a sequence.
You can combine the two in several different ways, none of which is
the "one most obvious". Python's OrderedDict retains the order of its
keys; in contrast, a namedtuple is a "record" type with a fixed set of
keys that correspond to positions in the record. In your case, I think
a namedtuple would be a good fit, but there's no general concept of
indexing a sequence with strings.

ChrisA


More information about the Python-list mailing list