142
New! Save questions or answers and organize your favorite content.
Learn more.
I receive a dictionary as input, and would like to to return a dictionary whose keys will be the input's values and whose value will be the corresponding input keys. Values are unique.
For example, say my input is:
a = dict() a['one']=1 a['two']=2I would like my output to be:
{1: 'one', 2: 'two'}To clarify I would like my result to be the equivalent of the following:
res = dict() res[1] = 'one' res[2] = 'two'Any neat Pythonic way to achieve this?
asked Jun 23, 2009 at 10:53
Roee AdlerRoee Adler
32.9k32 gold badges103 silver badges132 bronze badges
3
Python 2:
res = dict((v,k) for k,v in a.iteritems())Python 3 (thanks to @erik):
res = dict((v,k) for k,v in a.items())
gorcajo
5341 gold badge6 silver badges11 bronze badges
answered Jun 23, 2009 at 11:00
8
new_dict = dict(zip(my_dict.values(), my_dict.keys()))
vvvvv
19.1k16 gold badges44 silver badges62 bronze badges
answered Jul 6, 2009 at 16:09
JavierJavier
59.5k8 gold badges77 silver badges126 bronze badges
7
From Python 2.7 on, including 3.0+, there's an arguably shorter, more readable version:
>>> my_dict = {'x':1, 'y':2, 'z':3} >>> {v: k for k, v in my_dict.items()} {1: 'x', 2: 'y', 3: 'z'}
airstrike
2,1811 gold badge23 silver badges26 bronze badges
answered Jul 6, 2009 at 16:36
SilentGhostSilentGhost
294k64 gold badges301 silver badges291 bronze badges
0
You can make use of dict comprehensions:
Python 3
res = {v: k for k, v in a.items()}Python 2
res = {v: k for k, v in a.iteritems()}Edited: For Python 3, use a.items() instead of a.iteritems(). Discussions about the differences between them can be found in iteritems in Python on SO.
answered Aug 4, 2013 at 13:21
AkavallAkavall
78.7k47 gold badges199 silver badges244 bronze badges
In [1]: my_dict = {'x':1, 'y':2, 'z':3}
Python 3
In [2]: dict((value, key) for key, value in my_dict.items()) Out[2]: {1: 'x', 2: 'y', 3: 'z'}Python 2
In [2]: dict((value, key) for key, value in my_dict.iteritems()) Out[2]: {1: 'x', 2: 'y', 3: 'z'}
answered Jul 6, 2009 at 15:43
sunqiangsunqiang
6,3541 gold badge31 silver badges32 bronze badges
8
The current leading answer assumes values are unique which is not always the case. What if values are not unique? You will loose information! For example:
d = {'a':3, 'b': 2, 'c': 2} {v:k for k,v in d.iteritems()}returns {2: 'b', 3: 'a'}.
The information about 'c' was completely ignored. Ideally it should had be something like {2: ['b','c'], 3: ['a']}. This is what the bottom implementation does.
Python 2.x
def reverse_non_unique_mapping(d): dinv = {} for k, v in d.iteritems(): if v in dinv: dinv[v].append(k) else: dinv[v] = [k] return dinvPython 3.x
def reverse_non_unique_mapping(d): dinv = {} for k, v in d.items(): if v in dinv: dinv[v].append(k) else: dinv[v] = [k] return dinv
giotto
4523 silver badges14 bronze badges
answered Aug 3, 2017 at 23:20
Hanan ShteingartHanan Shteingart
7,8998 gold badges47 silver badges62 bronze badges
2
You could try:
Python 3
d={'one':1,'two':2} d2=dict((value,key) for key,value in d.items()) d2 {'two': 2, 'one': 1}Python 2
d={'one':1,'two':2} d2=dict((value,key) for key,value in d.iteritems()) d2 {'two': 2, 'one': 1}Beware that you cannot 'reverse' a dictionary if
- More than one key shares the same value. For example {'one':1,'two':1}. The new dictionary can only have one item with key 1.
- One or more of the values is unhashable. For example {'one':[1]}. [1] is a valid value but not a valid key.
See this thread on the python mailing list for a discussion on the subject.
answered Jun 23, 2009 at 11:02
AlasdairAlasdair
286k52 gold badges547 silver badges498 bronze badges
1
res = dict(zip(a.values(), a.keys()))
answered Jun 23, 2009 at 10:55
pkitpkit
7,7336 gold badges34 silver badges36 bronze badges
5
new_dict = dict( (my_dict[k], k) for k in my_dict)
or even better, but only works in Python 3:
new_dict = { my_dict[k]: k for k in my_dict}answered Jul 6, 2009 at 15:46
balpha♦balpha
48.8k17 gold badges112 silver badges129 bronze badges
1
Another way to expand on Ilya Prokin's response is to actually use the reversed function.
dict(map(reversed, my_dict.items()))In essence, your dictionary is iterated through (using .items()) where each item is a key/value pair, and those items are swapped with the reversed function. When this is passed to the dict constructor, it turns them into value/key pairs which is what you want.
answered Mar 9, 2016 at 23:53
Sunny PatelSunny Patel
7,5842 gold badges32 silver badges42 bronze badges
Suggestion for an improvement for Javier answer :
dict(zip(d.values(),d))Instead of d.keys() you can write just d, because if you go through dictionary with an iterator, it will return the keys of the relevant dictionary.
Ex. for this behavior :
d = {'a':1,'b':2} for k in d: k 'a' 'b'
Sparkup
3,6312 gold badges35 silver badges49 bronze badges
answered Jul 28, 2011 at 7:53
shadow2097shadow2097
711 silver badge3 bronze badges
Can be done easily with dictionary comprehension:
{d[i]:i for i in d}
juzraai
5,5058 gold badges31 silver badges46 bronze badges
answered Jul 15, 2018 at 14:48
1
dict(map(lambda x: x[::-1], YourDict.items()))
.items() returns a list of tuples of (key, value). map() goes through elements of the list and applies lambda x:[::-1] to each its element (tuple) to reverse it, so each tuple becomes (value, key) in the new list spitted out of map. Finally, dict() makes a dict from the new list.
Will
23.2k13 gold badges93 silver badges105 bronze badges
answered Aug 7, 2015 at 21:09
Ilya ProkinIlya Prokin
6446 silver badges10 bronze badges
1
Hanan's answer is the correct one as it covers more general case (the other answers are kind of misleading for someone unaware of the duplicate situation). An improvement to Hanan's answer is using setdefault:
mydict = {1:a, 2:a, 3:b} result = {} for i in mydict: result.setdefault(mydict[i],[]).append(i) print(result) >>> result = {a:[1,2], b:[3]}answered Apr 14, 2020 at 5:28
pegahpegah
6718 silver badges15 bronze badges
Using loop:-
newdict = {} #Will contain reversed key:value pairs. for key, value in zip(my_dict.keys(), my_dict.values()): # Operations on key/value can also be performed. newdict[value] = keyanswered May 3, 2014 at 7:05
1
If you're using Python3, it's slightly different:
res = dict((v,k) for k,v in a.items())answered Aug 9, 2014 at 17:42
Gravity GraveGravity Grave
2,6751 gold badge26 silver badges38 bronze badges
Adding an in-place solution:
>>> d = {1: 'one', 2: 'two', 3: 'three', 4: 'four'} >>> for k in list(d.keys()): ... d[d.pop(k)] = k ... >>> d {'two': 2, 'one': 1, 'four': 4, 'three': 3}In Python3, it is critical that you use list(d.keys()) because dict.keys returns a view of the keys. If you are using Python2, d.keys() is enough.
answered Apr 15, 2016 at 7:17
timgebtimgeb
74.7k20 gold badges115 silver badges141 bronze badges
I find this version the most comprehensive one:
a = {1: 'one', 2: 'two'}
swapped_a = {value : key for key, value in a.items()}
print(swapped_a)
output : {'one': 1, 'two': 2}
answered Jan 21, 2021 at 8:23
An alternative that is not quite as readable (in my opinion) as some of the other answers:
new_dict = dict(zip(*list(zip(*old_dict.items()))[::-1]))
where list(zip(*old_dict.items()))[::-1] gives a list of 2 tuples, old_dict's values and keys, respectively.
answered Oct 14, 2021 at 5:20
LuWilLuWil
363 bronze badges
2