How to "zip" two arrays into hash

11,832

Solution 1

I would do it this way:

keys = ['BO','BR']
values = ['BOLIVIA','BRAZIL']

Hash[keys.zip(values)]
# => {"BO"=>"BOLIVIA", "BR"=>"BRAZIL"}

If you want symbols for keys, then:

Hash[keys.map(&:to_sym).zip(values)]
# => {:BO=>"BOLIVIA", :BR=>"BRAZIL"}

In Ruby 2.1.0 or higher, you could write these as:

keys.zip(values).to_h
keys.map(&:to_sym).zip(values).to_h

As of Ruby 2.5 you can use .transform_keys:

Hash[keys.zip(values)].transform_keys { |k| k.to_sym }

Solution 2

Just use the single Array of the twos, and then transpose it, and generate Hash:

keys = ['BO','BR']
values = ['BOLIVIA','BRAZIL']
Hash[[keys,values].transpose]
# => {"BO"=>"BOLIVIA", "BR"=>"BRAZIL"}

or for newer ruby version:

[keys,values].transpose.to_h

Solution 3

Ironically, if you just sprinkle some dots and underscores into your question, it just works:

I want to "zip" two arrays into_hash

ary1.zip(ary2).to_h
# => { 'BO' => 'BOLIVIA', 'BR' => 'BRAZIL' }

Actually, you specified in your output hash that the keys should be Symbols not Strings, so we need to convert them first:

ary1.map(&:to_sym).zip(ary2).to_h
# => { BO: 'BOLIVIA', BR: 'BRAZIL' }

Solution 4

Quite readable version would be:

keys = ['BO','BR']
values = ['BOLIVIA','BRAZIL']

keys.zip(values).each_with_object({}) do |(key, value), hash|
  hash[key.to_sym] = value
end
Share:
11,832

Related videos on Youtube

Brazhnyk Yuriy
Author by

Brazhnyk Yuriy

Updated on June 17, 2022

Comments

  • Brazhnyk Yuriy
    Brazhnyk Yuriy about 2 years

    I want to "zip" two arrays into a Hash.

    From:

    ['BO','BR']
    ['BOLIVIA','BRAZIL']
    

    To:

    {BO: 'BOLIVIA', BR:'BRAZIL'}
    

    How can I do it?

    • lurker
      lurker about 10 years
      Your resulting hash isn't valid syntax. Did you mean {'BO': 'BOLIVIA', 'BR':'BRAZIL'}?
    • Jörg W Mittag
      Jörg W Mittag about 10 years
      @lurker: The resulting hash is valid syntax, yours isn't. In a new style hash literal, the keys need to be Symbols which are valid Ruby identifiers. 'BO' is not a legal identifier (apostrophes are not allowed in an identifier).
    • Peter DeWeese
      Peter DeWeese over 5 years
      Both are actually valid syntax, but if I "zipped" two arrays I'd expect the result to match the source.
  • toro2k
    toro2k about 10 years
    There is a reason to prefer [keys, values].transpose over keys.zip(values)?
  • Mike H-R
    Mike H-R about 10 years
    out of interest, why was this downvoted? seems like a valid method to me, it may run slow for larger lists but works fine.
  • Малъ Скрылевъ
    Малъ Скрылевъ about 10 years
    @toro2k that is graphically =)
  • samuil
    samuil about 10 years
    Biiig +1 for to_h. I'm not using 2.1.0 yet, but to_h is finally sane way to convert array of pairs to hash. Hash#[] method was really clunky and didn't have that Ruby feel of doing things.
  • Kris
    Kris almost 5 years
    You can now use transform_keys(&:to_sym) to convert to symbol keys.