# Converting a Vector{Vector{String}} to Dict{String, String}

**URL:** https://discourse.julialang.org/t/converting-a-vector-vector-string-to-dict-string-string/107733
**Category:** New to Julia
**Tags:** question, dictionary
**Created:** [December 17, 2023, 12:06pm UTC](https://discourse.julialang.org/t/converting-a-vector-vector-string-to-dict-string-string/107733 "2023-12-17T12:06:44Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Cortexture](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@Cortexture](https://discourse.julialang.org/u/Cortexture)
#### Post date: [December 17, 2023, 12:06pm UTC](https://discourse.julialang.org/t/converting-a-vector-vector-string-to-dict-string-string/107733/1 "2023-12-17T12:06:44Z")

</div>

```julia
using Pipe

text_vector = open("../data/exploring_the_wonders_of_nature.txt") |> readlines
words = replace(text_vector[1],
                  "," => "", "." => "",
                  "—" => " ") |> split .|> String
cmu_dataset = open("../data/cmudict-0.7b") |> readlines
entries_vector = @pipe split.(cmu_dataset, " ") |> map.(String, _)

```

Trying to convert words to their ARPAbet equivalents using the CMU Pronunciation Dictionary. entries\_vector is the Vector{Vector{String}}. A vector of words, then a vector with the word and it’s ARPAbet equivalent. Would like to create a dictionary with the word as a key, and it’s ARPAbet value, as value.  
I am lost.

---

<div class="post-metadata">

### Author: ![Cortexture](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@Cortexture](https://discourse.julialang.org/u/Cortexture)
#### Post date: [December 17, 2023, 12:11pm UTC](https://discourse.julialang.org/t/converting-a-vector-vector-string-to-dict-string-string/107733/2 "2023-12-17T12:11:10Z")

</div>

I came up with this solution, but I would like a more elegant solution. A more functional solution.

```julia
mykeys = Vector{String}()
myvalues = Vector{String}()
for entry ∈ entries_vector
    push!(mykeys, entry[1])
    push!(myvalues, entry[2])
end
entries = Dict(mykeys .=> myvalues)

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [December 17, 2023, 12:37pm UTC](https://discourse.julialang.org/t/converting-a-vector-vector-string-to-dict-string-string/107733/3 "2023-12-17T12:37:39Z")

</div>

> [@Cortexture](#):
>
> A vector of words, then a vector with the word and it’s ARPAbet equivalent.

Then the first vector is redundant, right? You just need the second? But it’s not clear how it’s organized. Can you cut and paste a bit of the data, a few entries, so we can see the expected input and output?

---

<div class="post-metadata">

### Author: ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)
#### Post date: [December 17, 2023, 12:59pm UTC](https://discourse.julialang.org/t/converting-a-vector-vector-string-to-dict-string-string/107733/4 "2023-12-17T12:59:19Z")

</div>

Does  
`Dict([entries_vector[1][i] => entries_vector[2][i] for i in eachindex(entries_vector[1])])`  
Do what you want? I am on my phone, so I can not test it. I have made a strong assumption abou the two vectors in `entries_vector` being of equal length.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [December 17, 2023, 2:55pm UTC](https://discourse.julialang.org/t/converting-a-vector-vector-string-to-dict-string-string/107733/5 "2023-12-17T14:55:35Z")

</div>

> [@TheLateKronos](#):
>
> ```julia
> Dict([entries_vector[1][i] => entries_vector[2][i] for i in eachindex(entries_vector[1])])
> 
> ```

Remove the square brackets, they create an intermediate, and redundant, vector, before conversion to Dict.

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [December 17, 2023, 4:42pm UTC](https://discourse.julialang.org/t/converting-a-vector-vector-string-to-dict-string-string/107733/6 "2023-12-17T16:42:03Z")

</div>

Or simply:

```julia
Dict(i => j for (i,j) in entries_vector)

```

Since the first and second entries of each inner vector should always be a word and its ARPAbet pronunciation, respectively.

---

<div class="post-metadata">

### Author: ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)
#### Post date: [December 18, 2023, 4:32pm UTC](https://discourse.julialang.org/t/converting-a-vector-vector-string-to-dict-string-string/107733/7 "2023-12-18T16:32:21Z")

</div>

It actually appears that an iterator over 2-tuples works as well. So

```julia
Dict(zip(entries_vector...))

```

Is quite consise and readable, and should do the same thing without a comprehension  
Disclaimer: On phone, not tested.
