{}.items() backwards
Hi! I'm trying to find the best way to run the {}.items() function "backwards", that is, something like this
(in doctest lingo):
>>> myList = [('moose', 'Bullwinkle'), ('squirrel', 'Rocky')]
>>> backwardsItems(myList)
{'moose': 'Bullwinkle', 'squirrel': 'Rocky'}
I could do it by hand, of course, like
But I can't think of any. Your thoughts? Thanks!
[EDIT: When I posted this, I mistakenly used a comma instead of a colon after 'squirrel', making it totally confusing about what I wanted to do. Oops.]
(in doctest lingo):
>>> myList = [('moose', 'Bullwinkle'), ('squirrel', 'Rocky')]
>>> backwardsItems(myList)
{'moose': 'Bullwinkle', 'squirrel': 'Rocky'}
I could do it by hand, of course, like
def backwardsItems():... but I wonder if that isn't slow when I'm doing it again and again to some large dictionaries, and I can't help but think there might be a built-in function for it, or at least some elegant and Pythonic way.
myDict = {}
for myTuple in myList:
myDict[myTuple[0]] = myTuple[1]
return myDict
But I can't think of any. Your thoughts? Thanks!
[EDIT: When I posted this, I mistakenly used a comma instead of a colon after 'squirrel', making it totally confusing about what I wanted to do. Oops.]
