# Conversion of an array of tuples into a dictionary

**URL:** <https://discourse.julialang.org/t/conversion-of-an-array-of-tuples-into-a-dictionary/68329>\
**Category:** New to Julia\
**Tags:** tuple, dictionary, convert\
**Created:** [September 17, 2021, 12:12pm UTC](https://discourse.julialang.org/t/conversion-of-an-array-of-tuples-into-a-dictionary/68329 "2021-09-17T12:12:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbacelar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbacelar/32/4359_2.png) [@sbacelar](https://discourse.julialang.org/u/sbacelar)\
**Post date:** [September 17, 2021, 12:12pm UTC](https://discourse.julialang.org/t/conversion-of-an-array-of-tuples-into-a-dictionary/68329/1 "2021-09-17T12:12:19Z")

</div>

Sorry for this easy question but I’m lost.  
I want to convert this array of tuples:

```julia
ls = [(['A' 'B'], 1)

        (['Z'], 2)]

```

into this dictionary  
`Dict('A' => 1, 'B' => 1, 'Z' => 2)`  
I tried this  
`d1 = [ls[i][1] .=> ls[i][2] for i in 1:length(ls)]`  
and this  
`d2 = [map(x -> x => ls[i][2], collect(ls[i][1])) for i in 1:length(ls)]`  
both without success.

---

<div class="post-metadata">

**Author:** ![Kreiton\_Sitorus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kreiton_sitorus/32/29268_2.png) [@Kreiton\_Sitorus](https://discourse.julialang.org/u/Kreiton_Sitorus)\
**Post date:** [September 17, 2021, 12:31pm UTC](https://discourse.julialang.org/t/conversion-of-an-array-of-tuples-into-a-dictionary/68329/2 "2021-09-17T12:31:19Z")

</div>

Dictionaries are pairs of keys and values. So, in order to produce `Dict('A' => 1, 'B' => 1, 'Z' => 2)` you need to have an array of tuple that represents that – meaning a value for every key.

```julia
julia> ls = [("A",1), ("B",1), ("Z",2)]
3-element Vector{Tuple{String, Int64}}:
 ("A", 1)
 ("B", 1)
 ("Z", 2)

julia> Dict(ls)
Dict{String, Int64} with 3 entries:
  "Z" => 2
  "B" => 1
  "A" => 1

```

separating `(["A","B"], 1)` into `("A",1)` and `("B",1)` in your array should do the job.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 17, 2021, 12:41pm UTC](https://discourse.julialang.org/t/conversion-of-an-array-of-tuples-into-a-dictionary/68329/3 "2021-09-17T12:41:48Z")

</div>

> [@sbacelar](#):
>
> ```julia
> ls = [(['A' 'B'], 1), (['Z'], 2)]
> 
> ```

Just write a nested loop:

```julia
d = Dict{Char,Int}()
for (keys, val) in ls, key in keys
    d[key] = val
end

```

Higher-order facilities like `map` and broadcasting are great, and are often very convenient, but sometimes an explicit loop is the clearest way to do something. (And loops are fast in Julia; for people coming from languages like Python or Matlab, one has to unlearn the instinct to avoid explicit loops at all costs.)

PS. Note that `['A' 'B']` in Julia is a 1×2 matrix of `Char`. This works fine with my loops here, but you probably wanted a 1d array, which is denoted `['A', 'B']` (i.e., with a comma).

---

<div class="post-metadata">

**Author:** ![sbacelar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbacelar/32/4359_2.png) [@sbacelar](https://discourse.julialang.org/u/sbacelar)\
**Post date:** [September 17, 2021, 12:44pm UTC](https://discourse.julialang.org/t/conversion-of-an-array-of-tuples-into-a-dictionary/68329/4 "2021-09-17T12:44:21Z")

</div>

Fantastic!

---

<div class="post-metadata">

**Author:** ![Kreiton\_Sitorus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kreiton_sitorus/32/29268_2.png) [@Kreiton\_Sitorus](https://discourse.julialang.org/u/Kreiton_Sitorus)\
**Post date:** [September 17, 2021, 12:46pm UTC](https://discourse.julialang.org/t/conversion-of-an-array-of-tuples-into-a-dictionary/68329/5 "2021-09-17T12:46:00Z")

</div>

just realized @sbacelar really meant the exact array of tuples 😬 so sorryy 🙁
