Make an inversedict
Mathematica, 28 bytes
Merge[Reverse/@Normal@#,#&]&
The input is an Association
, the output is an Association
too.
Explanation
Normal@#
Convert the input Association
to a List
of Rule
s.
Reverse/@ ...
Reverse the Rule
s.
Merge[ ... ,#&]
Merge
all Rule
s, grouping duplicates with a List
and then with the identity operation. Creates an Association
.
Usage
Merge[Reverse/@Normal@#,#&]&[
<|"Clyde" -> "blue", "Sarah" -> "blue", "Fred" -> "green"|>
]
<|"blue" -> {"Clyde", "Sarah"}, "green" -> {"Fred"}|>
Python 2, 54 bytes
lambda i:{i[k]:[a for a in i if i[a]==i[k]]for k in i}
Usage:
f=lambda i:{i[k]:[a for a in i if i[a]==i[k]]for k in i}
print f({"Clyde": "blue", "Sarah": "blue", "Fred": "green"})
Gives
{'blue': ['Sarah', 'Clyde'], 'green': ['Fred']}
Brachylog, 10 bytes
tᵍ⟨ttʰhᵐ⟩ᵐ
Try it online!
Takes input as a list of [key, value]
pairs through the input variable, and outputs a list of [value, [keys]]
pairs through the output variable.
ᵍ Group the elements of
the input
t by their last elements.
ᵐ For each group,
t its last element
⟨ ⟩ paired with
hᵐ the first element of every element
ʰ with the first element of the pair being replaced by
t its last element
ᵐ is the corresponding element of
the output.
...such an eloquent explanation...