Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8086663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:31:26+00:00 2026-06-05T18:31:26+00:00

How do I swap keys and values in a Hash? I have the following

  • 0

How do I swap keys and values in a Hash?

I have the following Hash:

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

that I want to transform into:

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

Using map seems rather tedious. Is there a shorter solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:31:28+00:00Added an answer on June 5, 2026 at 6:31 pm

    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
      end
    end
    

    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}
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to swap the values from two rows in a table. I have
Can I swap the keys of two values of a Hashmap, or do I
I have to swap two variables with a number value without using a third
I want to swap the value of two integer variables in java using the
I want to swap rows between gridviews by dragging one row from gridview1 to
I'm trying to swap two variables in ActionScript. I tried using: a = 42
I want to swap out one view with another by pushing the old view
How does one swap the axes in a WPF Toolkit line chart? I have
I want to swap two integers, and I want to know which of these
I want to swap to tables in the best possible manner. I have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.