Dictionary from list?

Martin von Loewis loewis at informatik.hu-berlin.de
Sun Oct 21 08:35:25 EDT 2001


Jim Correia <jim.correia at pobox.com> writes:

> I was just asking if there was a built in coercion available.  
> (Coercions aren't unheard of in scripting languages.)  

And indeed, Python does have conversions. Since variables are untyped,
it never does them on assignment. Also, this particular conversion is
not supported in the standard library. If there would be a frequent
need for it, it probably would be added.

Note that the list->dictionary conversion is not as obvious as you
think. Given

[(1,2),(3,4)]

would you like to get

a) {1:2, 3:4}

or

b) {(1,2) : (3,4)}

Your answer is probably b), but many Python people would rather expect
a) (since it would be the reverse of the .items() operation).

Regards,
Martin



More information about the Python-list mailing list