converting tuple to list

Fredrik Lundh fredrik at pythonware.com
Thu Jan 13 09:14:59 EST 2000


Adrian Eyre <a.eyre at optichrome.com> wrote:
> Just out of interest: what is the canonical way of converting a list
> of tuples to a dictionary? i.e. the opposite of dict.items()
> 
> [("key1","value1"),("key2","value2")] -> {"key1":"value1","key2":"value2"}

I doubt you can do much better than:

mydict = {}
for k, v in mylist:
    mydict[k] = v

</F>

<!-- (the eff-bot guide to) python patterns and idioms:
http://www.pythonware.com/people/fredrik/patternbook.htm
-->





More information about the Python-list mailing list