[Python-Dev] Type-converting functions, esp. unicode() vs. unistr()
Tim Peters
tim.one@home.com
Thu, 18 Jan 2001 22:46:16 -0500
[attribution lost]
> There is no built-in dict(); if it existed i suppose it would do
> the opposite of x.items(); again a weak argument, though i might
> have found such a function useful once or twice.
[Guido]
> Yeah, it's not very common. Dict comprehensions anyone?
>
> d = {k:v for k,v in zip(range(10), range(10))} # :-)
It's very common in Perl code, but is in no sense the inverse of .items()
there: when you build a dict from a list L in Perl, it acts like Python
{L[0]: L[1],
L[2]: L[3],
L[4]: L[5],
...
}
That's what seems most practical most often; e.g., when crunching over text
files with records of the form
key value
(e.g., mail headers are of this form; simple contact databases; to-do lists
segregated by date; etc), whatever fancy re.split() is used to break things
apart naturally returns a flat list. A list of two-tuples is natural only
if it was obtained from another dict's .items() <0.9 wink>.
pushing-the-limits-of-"practicality-beats-purity"?-ly y'rs - tim