How to turn a list of tuples into a dictionary?
castironpi at gmail.com
castironpi at gmail.com
Tue Feb 26 12:05:12 EST 2008
On Feb 26, 11:00 am, mrstephengross <mrstevegr... at gmail.com> wrote:
> Let's say I've got a list of tuples, like so:
>
> ( ('a', '1'), ('b', '2'), ('c', '3')
>
> And I want to turn it into a dictionary in which the first value of
> each tuple is a key and the second value is a value, like so:
>
> { 'a' -> '1', 'b' -> '2', 'c' -> '3' }
>
> Is there a way to do this with a single line of code? Currently, I'm
> doing it like this:
>
> tuples = ( ('a', '1'), ('b', '2'), ('c', '3')
> d = {}
> for option,value in tuples: d[option] = value
>
> Thanks,
> --Steve
I'd hand-code it manually, by linking a C extension. Or
dict( iterable ).
http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-21
More information about the Python-list
mailing list