3

I have a dictionary in which keys and values are to be swapped. For eg:

dic = {"indianapolis":"indiana", "columbus":"ohio", "jackson":"mississippi",
  "phoenix":"arizona"}

Output should be:

{"indiana":"indianapolis", "ohio":"columbus", "mississippi":"jackson",
  "arizona":"phoenix"}

Here is what i tried but the constraints are that only sorted(), split() functions should be used and no other built-in function should be used (like keys(), values(), items(),lambda). Not really sure how to proceed further. Can someone help ? I am new to dictionaries.

def interchange(input_dict):
    temp = {}
    dic = input_dict
    for i in dict:
        temp[i[1]] = i[0]
    return temp_dic
Shraddha
  • 183
  • 1
  • 2
  • 10
  • Another method is shown in this question: http://stackoverflow.com/questions/1087694/how-to-swap-keys-for-values-in-a-dictionary – Keeper Dec 08 '13 at 16:04
  • thank you but i should not use .items() or .iteritems(). That is no inbuilt functions. – Shraddha Dec 08 '13 at 16:07

4 Answers4

3

only .sorted(), .split() functions should be used

Using sorted and a dictionary comprehension:

>>> dic = {"indianapolis":"indiana", "columbus":"ohio", "jackson":"mississippi", "phoenix":"arizona"}
>>> {dic[k]:k for k in sorted(dic)}
{'ohio': 'columbus', 'mississippi': 'jackson', 'arizona': 'phoenix', 'indiana': 'indianapolis'}

sorted returns the keys of dictionary in sorted order, though the order of final dict is going to be arbitrary. The above code is equivalent to:

>>> sorted(dic)
['columbus', 'indianapolis', 'jackson', 'phoenix']
>>> new_dic = {}
>>> for k in sorted(dic):
...     new_dic[dic[k]] = k
...     
>>> new_dic
{'ohio': 'columbus', 'mississippi': 'jackson', 'arizona': 'phoenix', 'indiana': 'indianapolis'}

Update:

def interchange(input_dict):
    return {input_dict[k]:k for k in sorted(input_dict)}
... 
>>> interchange(dic)
{'ohio': 'columbus', 'mississippi': 'jackson', 'arizona': 'phoenix', 'indiana': 'indianapolis'}
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • Thanks. But how do i convert it to a "def interchange(input_dict)"function so that i input dic and it gives me a swapped dictionary? – Shraddha Dec 08 '13 at 16:13
  • Do you even need to use `sorted`? wouldn't `{input_dict[k]:k for k in input_dict}` give the same result? – Akavall Dec 08 '13 at 16:21
  • `sorted()` makes little sense here; the output is a dictionary, after all. The only difference it'll make is if there are duplicate values in the input dictionary; which key ends up in the output is then determined by insertion order. But even that depends on the specific Python implementation; the spec doesn't state what order keys are inserted in a dict comprehension. Practically, it'll be the last inserted key that wins, but that isn't *required* by the language specification. – Martijn Pieters Dec 08 '13 at 17:04
1

Using dict comprehension:

>>> dic = {"indianapolis":"indiana", "columbus":"ohio",
...        "jackson":"mississippi", "phoenix":"arizona"}
>>> {dic[key]: key for key in dic}
{'ohio': 'columbus', 'mississippi': 'jackson',
 'arizona': 'phoenix', 'indiana': 'indianapolis'}
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • thank you but i should not use .items() or .iteritems(). That is no inbuilt functions. – Shraddha Dec 08 '13 at 16:06
  • Thanks. But how do i convert it to a "def interchange(input_dict)"function so that i input dic and it gives me a swapped dictionary? – Shraddha Dec 08 '13 at 16:16
  • @Pramod, Just `def interchange(input_dict): return {input_dict[key]: key for key in input_dict}` – falsetru Dec 08 '13 at 16:18
0

Some nice option:

dic = {"indianapolis":"indiana", "columbus":"ohio", "jackson":"mississippi",
  "phoenix":"arizona"}

newDict = dict(zip(dic.values(),dic.keys()))

print newDict

Output:

{'ohio': 'columbus', 'mississippi': 'jackson', 'arizona': 'phoenix', 'indiana': 'indianapolis'}

Explanation:

zip() returns a list of tuples (dictionery), and the parameters for zip are reversed in order...

Without python methods:

dic = {"indianapolis":"indiana", "columbus":"ohio", "jackson":"mississippi",
  "phoenix":"arizona"}

newDict = {}

for key, item in dic.iteritems():
    newDict.update({item: key})

print newDict

Output:

{'ohio': 'columbus', 'mississippi': 'jackson', 'arizona': 'phoenix', 'indiana': 'indianapolis'}
Kobi K
  • 7,743
  • 6
  • 42
  • 86
0

very simple

tuple_list = zip(dic.values(),dic.keys())
Result = dict(tuple_list)

will produce the required output

{'arizona': 'phoenix',
 'indiana': 'indianapolis',
 'mississippi': 'jackson',
 'ohio': 'columbus'}

Edit One line solution

{ dic[Key]:Key for Key in list(dic) }
securecurve
  • 5,589
  • 5
  • 45
  • 80