How to swap keys and values in a hash How to swap keys and values in a hash ruby ruby

How to swap keys and values in a hash


Ruby has a helper method for Hash that lets you treat a Hash as if it was inverted (in essence, by letting you access keys through values):

{a: 1, b: 2, c: 3}.key(1)=> :a

If you want to keep the inverted hash, then Hash#invert should work for most situations:

{a: 1, b: 2, c: 3}.invert=> {1=>:a, 2=>:b, 3=>:c}

BUT...

If you have duplicate values, invert will discard all but the last occurrence of your values (because it will keep replacing new value for that key during iteration). Likewise, key will only return the first match:

{a: 1, b: 2, c: 2}.key(2)=> :b{a: 1, b: 2, c: 2}.invert=> {1=>:a, 2=>:c}

So, if your values are unique you can use Hash#invert. If not, then you can keep all the values as an array, like this:

class Hash  # like invert but not lossy  # {"one"=>1,"two"=>2, "1"=>1, "2"=>2}.inverse => {1=>["one", "1"], 2=>["two", "2"]}   def safe_invert    each_with_object({}) do |(key,value),out|       out[value] ||= []      out[value] << key    end  endend

Note: This code with tests is now on GitHub.

Or:

class Hash  def safe_invert    self.each_with_object({}){|(k,v),o|(o[v]||=[])<<k}  endend


You bet there is one! There is always a shorter way to do things in Ruby!

It's pretty simple, just use Hash#invert:

{a: :one, b: :two, c: :three}.invert=> {:one=>:a, :two=>:b, :three=>:c}

Et voilĂ !


files = {  'Input.txt' => 'Randy',  'Code.py' => 'Stan',  'Output.txt' => 'Randy'}h = Hash.new{|h,k| h[k] = []} # Create hash that defaults unknown keys to empty an empty listfiles.map {|k,v| h[v]<< k} #append each key to the list at a known valueputs h

This will handle the duplicate values too.