# How to convert a Dict to a Vector of NamedTuples?

I am trying to do the following

``````julia> dict = Dict(1 => Dict("a"=> (n=1.9,t=2), "b" => (n=3.1,t=5) ),
2 => Dict("a"=> (n=6.6,t=5), "b" => (n=3.9,t=6) ));

julia> tbl = [(jt=k,case=cs,v[cs]...) for (k, v) in dict, cs in ("a","b") ]
``````

I get

``````2×2 Array{NamedTuple{(:jt, :case, :n, :t),Tuple{Int64,String,Float64,Int64}},2}:
(jt = 2, case = "a", n = 6.6, t = 5)  (jt = 2, case = "b", n = 3.9, t = 6)
(jt = 1, case = "a", n = 1.9, t = 2)  (jt = 1, case = "b", n = 3.1, t = 5)
``````

Desired output:

``````4-element Array{NamedTuple,1}:
(jt = 2, case = "a", n = 6.6, t = 5)
(jt = 2, case = "b", n = 3.9, t = 6)
(jt = 1, case = "a", n = 1.9, t = 2)
(jt = 1, case = "b", n = 3.1, t = 5)
``````

Swap a comma for an extra `for`:

``````julia> tbl = [(jt=k,case=cs,v[cs]...) for (k, v) in dict for cs in ("a","b") ]
4-element Array{NamedTuple{(:jt, :case, :n, :t),Tuple{Int64,String,Float64,Int64}},1}:
(jt = 2, case = "a", n = 6.6, t = 5)
(jt = 2, case = "b", n = 3.9, t = 6)
(jt = 1, case = "a", n = 1.9, t = 2)
(jt = 1, case = "b", n = 3.1, t = 5)
``````
1 Like