# How to handle an array of dictionaries?

**URL:** <https://discourse.julialang.org/t/how-to-handle-an-array-of-dictionaries/69649>\
**Category:** General Usage\
**Created:** [October 12, 2021, 11:19pm UTC](https://discourse.julialang.org/t/how-to-handle-an-array-of-dictionaries/69649 "2021-10-12T23:19:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![trg818](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trg818/32/219149_2.png) [@trg818](https://discourse.julialang.org/u/trg818)\
**Post date:** [October 12, 2021, 11:19pm UTC](https://discourse.julialang.org/t/how-to-handle-an-array-of-dictionaries/69649/1 "2021-10-12T23:19:35Z")

</div>

It’s actually even more convoluted: I have a dictionary `rspecs` and want to allocate one of its elements, with key `rname`, as an array of dictionaries, whereby the array is to have length `nw`. This is what I tried:

```julia
roomspecs[rname]=Array{Dict{String,Any}}(undef,nw)
for j in range(1,stop=nw)
# here comes some stuff to assign a value to mat
   roomspecs[rname][j]["material"]=mat
end

```

Although it seems that an array of dicts of the correct length is created, the attempt to assign values to its entries shown in the loop fails:

```julia
ERROR: LoadError: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex(A::Vector{Dict{String, Any}}, i1::Int64)
   @ Base ./array.jl:801
 [2] (::var"#1#2")(f::IOStream)
   @ Main ~/texts/admin/private/house/heatflow.jl:58

```

What is wrong here?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 12, 2021, 11:37pm UTC](https://discourse.julialang.org/t/how-to-handle-an-array-of-dictionaries/69649/2 "2021-10-12T23:37:01Z")

</div>

You are not initializing the array of `Dict`s. You explicitly say `(undef, nw)`, so each position in `roomspecs` has a `Vector` that is a valid object itself but has `nw` positions with garbage (you did not created the `Dict` objects and assigned them to the vectors). You can probably do:

```julia
nw = 10
rname = 1
roomspecs = Vector{Any}(undef, 1)
roomspecs[rname]=Array{Dict{String,Any}}(undef,nw)
for j in range(1,stop=nw)
       # here comes some stuff to assign a value to mat
       roomspecs[rname][j] = Dict{String, Any}()
       roomspecs[rname][j]["material"] = "mat"
end

```

---

<div class="post-metadata">

**Author:** ![trg818](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trg818/32/219149_2.png) [@trg818](https://discourse.julialang.org/u/trg818)\
**Post date:** [October 12, 2021, 11:43pm UTC](https://discourse.julialang.org/t/how-to-handle-an-array-of-dictionaries/69649/3 "2021-10-12T23:43:15Z")

</div>

Yes, thanks, that seems to work! I thought I had created the Dict objects with the `Array{Dict{String,Any}}` part…

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 13, 2021, 12:09am UTC](https://discourse.julialang.org/t/how-to-handle-an-array-of-dictionaries/69649/4 "2021-10-13T00:09:07Z")

</div>

The `Dict{String, Any}` part inside `Array{...}` is just specifying the type of the elements of the `Array`. If you create an empty `Array` this means you cannot `push!(array, element)` if `element` is not of the right type. But this does not create empty objects for you, even so because some types may not have an empty constructor.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [October 14, 2021, 8:45am UTC](https://discourse.julialang.org/t/how-to-handle-an-array-of-dictionaries/69649/6 "2021-10-14T08:45:04Z")

</div>

A more Julian way to do that would be

```julia
roomspecs[rname]=[Dict{Symbol,Any}() for _ in 1:nw]
for j in 1:nw
# here comes some stuff to assign a value to mat
   roomspecs[rname][j][:material] = mat
end

```

1. An array comprehension `[Dict{...}() for _ in 1:nw]` creates an array initialized with a new object in each cell
2. If your keys are not user-provided but can only be from a pre-defined set, using `Symbol`s is more appropriate.
