Top.Mail.Ru
Python
? ?
LiveJournal for python.
View:Personal Journal.
View:Friends.
View:Calendar.
Missed some entries? Then simply jump to the previous day or the next day.

Wednesday, December 29th, 2004

Subject:{}.items() backwards
Time:9:50 pm.
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
def backwardsItems():
    myDict = {}
    for myTuple in myList:
        myDict[myTuple[0]] = myTuple[1]
    return myDict
... 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.

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.]
Comments: Read 7 orAdd Your Own.

LiveJournal for python.

View:User Info.
View:Friends.
View:Calendar.
View:Memories.
Missed some entries? Then simply jump to the previous day or the next day.

Image