# Merge two arrays into a hash

**URL:** <https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560>\
**Category:** Ruby\
**Created:** [September 26, 2007, 8:47am UTC](https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560 "2007-09-26T08:47:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [September 26, 2007, 8:47am UTC](https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560/1 "2007-09-26T08:47:02Z")

</div>

Hi all,

I have looked at some of the answers in the forum, but they do not seem  
to fit. I want to merge two array into a hash like so:

key = [“1”, “2”, “3”]  
value = [“a”, “b”]

into:

myhash = {‘1’ =\> ‘a’ , ‘2’ =\> ‘b’ , ‘3’ =\> ‘’}

Can this be done without creating new classes and methods?

Thanks a lot

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [September 26, 2007, 8:58am UTC](https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560/2 "2007-09-26T08:58:11Z")

</div>

irb(main):001:0\> key = [“1”, “2”, “3”]  
=\> [“1”, “2”, “3”]  
irb(main):002:0\> value = [“a”, “b”]  
=\> [“a”, “b”]  
irb(main):003:0\> myhash = {}  
=\> {}  
irb(main):004:0\> key.each\_with\_index {|k,i|myhash[k] = value[i]}  
=\> [“1”, “2”, “3”]  
irb(main):005:0\> myhash  
=\> {“1”=\>“a”, “2”=\>“b”, “3”=\>nil}

On 9/26/07, Jan A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> myhash = {‘1’ =\> ‘a’ , ‘2’ =\> ‘b’ , ‘3’ =\> ‘’}
> 
> Can this be done without creating new classes and methods?
> 
> ## Thanks a lot
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–

“Every child has many wishes. Some include a wallet, two chicks and a  
cigar, but that’s another story.”

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [September 26, 2007, 9:02am UTC](https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560/3 "2007-09-26T09:02:16Z")

</div>

Alle mercoledÃ¬ 26 settembre 2007, Jan A. ha scritto:

> myhash = {‘1’ =\> ‘a’ , ‘2’ =\> ‘b’ , ‘3’ =\> ‘’}
> 
> Can this be done without creating new classes and methods?
> 
> Thanks a lot

require ‘generator’  
h = {}  
SyncEnumerator.new(key, value).each{|i| h[i[0]] = (i[1]|| “”)}  
p h  
=\> {‘1’=\>‘a’, ‘2’=\>‘b’, ‘3’=\>‘’}

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [September 26, 2007, 9:03am UTC](https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560/4 "2007-09-26T09:03:26Z")

</div>

On 26/09/2007, Jan A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> myhash = {‘1’ =\> ‘a’ , ‘2’ =\> ‘b’ , ‘3’ =\> ‘’}
> 
> Can this be done without creating new classes and methods?

irb(main):001:0\> key = [“1”,“2”,“3”]  
=\> [“1”, “2”, “3”]  
irb(main):002:0\> value = [“a”,“b”,“c”]  
=\> [“a”, “b”]  
irb(main):009:0\> Hash[\*key.zip(value).flatten]  
=\> {“1”=\>“a”, “2”=\>“b”, “3”=\>nil}

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [September 26, 2007, 9:05am UTC](https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560/5 "2007-09-26T09:05:16Z")

</div>

Thanks to all of you!

I now have a beautiful hash that my mother would be proud of 🙂

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [September 26, 2007, 9:06am UTC](https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560/6 "2007-09-26T09:06:58Z")

</div>

> key = [“1”, “2”, “3”]  
> value = [“a”, “b”]
> 
> into:
> 
> myhash = {‘1’ =\> ‘a’ , ‘2’ =\> ‘b’ , ‘3’ =\> ‘’}

For the special case where key and value do not contain arrays:

Hash[\*key.zip(value).flatten]

For the general case:

myhash = {}  
key.zip(value) {|a,b| myhash[a] = b }

Dan

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [September 26, 2007, 9:08am UTC](https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560/7 "2007-09-26T09:08:09Z")

</div>

> require ‘generator’  
> h = {}  
> SyncEnumerator.new(key, value).each{|i| h[i[0]] = (i[1]|| “”)}  
> p h  
> =\> {‘1’=\>‘a’, ‘2’=\>‘b’, ‘3’=\>‘’}

When an array is being passed to a block, you can just provide args for  
the whole array:

SyncEnumerator.new(key, value).each{|k,v| h[k] = v||‘’}

Gives you the same result.

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [September 26, 2007, 9:49am UTC](https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560/8 "2007-09-26T09:49:10Z")

</div>

> > myhash = {}  
> > key.zip(value) {|a,b| myhash[a] = b }
> > 
> > It does not work!  
> > irb(main):017:0\> key.zip(value){|a,b| myhash[a] = b}  
> > =\> nil

The result is in my\_hash

irb(main):014:0\> myhash = {}  
=\> {}  
irb(main):015:0\> key.zip(value) {|a,b| myhash[a] = b }  
=\> nil  
irb(main):016:0\> myhash  
=\> {“1”=\>“a”, “2”=\>“b”, “3”=\>nil}

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [September 26, 2007, 9:24am UTC](https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560/9 "2007-09-26T09:24:36Z")

</div>

On 9/26/07, Daniel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hash[\*key.zip(value).flatten]
> 
> For the general case:
> 
> myhash = {}  
> key.zip(value) {|a,b| myhash[a] = b }
> 
> Dan
> 
> It does not work!  
> irb(main):017:0\> key.zip(value){|a,b| myhash[a] = b}  
> =\> nil

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [September 26, 2007, 10:09am UTC](https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560/10 "2007-09-26T10:09:39Z")

</div>

Hi –

On Wed, 26 Sep 2007, Daniel S. wrote:

> > key = [“1”, “2”, “3”]  
> > value = [“a”, “b”]
> > 
> > into:
> > 
> > myhash = {‘1’ =\> ‘a’ , ‘2’ =\> ‘b’ , ‘3’ =\> ‘’}
> 
> For the special case where key and value do not contain arrays:
> 
> Hash[\*key.zip(value).flatten]

And in 1.9 you can do: flatten(1)

David

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [September 26, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560/11 "2007-09-26T16:11:01Z")

</div>

On Sep 26, 2:09 am, “David A. Black” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And in 1.9 you can do: flatten(1)

Hot dog! Come on lucky number 1.9, you’re just what I wanted!

(Seriously, I’ve used David’s flatten\_n library many times in the  
past, but kept trying to wean myself from it since it wasn’t in the  
core or stdlib. How nice it will be to have that functionality  
generally available!)

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [September 26, 2007, 9:57am UTC](https://www.ruby-forum.com/t/merge-two-arrays-into-a-hash/115560/12 "2007-09-26T09:57:21Z")

</div>

On 9/26/07, Daniel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb(main):014:0\> myhash = {}  
> =\> {}  
> irb(main):015:0\> key.zip(value) {|a,b| myhash[a] = b }  
> =\> nil  
> irb(main):016:0\> myhash  
> =\> {“1”=\>“a”, “2”=\>“b”, “3”=\>nil}

Thanks a lot! By the way, what is the different between special case and  
general case?

For the special case where key and value do not contain arrays:

Hash[\*key.zip(value).flatten]

For the general case:

myhash = {}  
key.zip(value) {|a,b| myhash[a] = b }

Dan
