View Single Post
Old Mar 8th, 2005, 12:45 PM   #5
block01cube
Newbie
 
Join Date: Feb 2005
Posts: 10
Rep Power: 0 block01cube is on a distinguished road
Quote:
Originally Posted by Dietrich
If you had made an Englishutch dictionary, how could you convert that to a Dutch:English dictionary? Any ideas?
If you mean how do you change a dictionary from keys:items to items:keys, try this:

def reversedict(original_dict):

    dict2 = {}

    dict_list = original_dict.items()

    for i in dict_list:
        dict2[i[1]] = i[0]
    
    return dict2

# Dict 1 is just for the purpose of example, you could feed this function
# Any dictionary.

dict1 = {1: 'a', 2: 'b', 3: 'c', 4: 'd'}

dict3 = reversedict(dict1)

print dict3
block01cube is offline   Reply With Quote