# Extracting elements from dictionary containing a struct, where the struct is made up of a dictionary containing a nested struct

**URL:** https://discourse.julialang.org/t/extracting-elements-from-dictionary-containing-a-struct-where-the-struct-is-made-up-of-a-dictionary-containing-a-nested-struct/77288
**Category:** General Usage
**Tags:** question
**Created:** [March 2, 2022, 10:27am UTC](https://discourse.julialang.org/t/extracting-elements-from-dictionary-containing-a-struct-where-the-struct-is-made-up-of-a-dictionary-containing-a-nested-struct/77288 "2022-03-02T10:27:00Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Christian\_Brunbjerg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christian_brunbjerg/32/34243_2.png) [@Christian\_Brunbjerg](https://discourse.julialang.org/u/Christian_Brunbjerg)
#### Post date: [March 2, 2022, 10:27am UTC](https://discourse.julialang.org/t/extracting-elements-from-dictionary-containing-a-struct-where-the-struct-is-made-up-of-a-dictionary-containing-a-nested-struct/77288/1 "2022-03-02T10:27:00Z")

</div>

```julia
#Initializing the 2 structs
struct inner
    value::Float64
end

struct outer
    inner::Any
end

#Initializing the 2 dictionaries
outer_dic = Dict()
inner_dic = Dict()

#Defining the values for the inner dictionary
inner_dic[1] = inner(1)
inner_dic[2] = inner(2)
inner_dic[3] = inner(3)

#Defining the values for the outer dictionary
outer_dic[1] = outer([inner_dic[1],inner_dic[2],inner_dic[3]])

#Goal
#This does not work but this is what I would like to do as simple as possible, preferably without for-loops
array = collect(outer_dic[1].inner[1:3].value)

```

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 2, 2022, 11:46am UTC](https://discourse.julialang.org/t/extracting-elements-from-dictionary-containing-a-struct-where-the-struct-is-made-up-of-a-dictionary-containing-a-nested-struct/77288/2 "2022-03-02T11:46:53Z")

</div>

```julia
@show getfield.((outer_dic[1].inner[i] for i in 1:3), :value)

```

works for me.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [March 2, 2022, 12:01pm UTC](https://discourse.julialang.org/t/extracting-elements-from-dictionary-containing-a-struct-where-the-struct-is-made-up-of-a-dictionary-containing-a-nested-struct/77288/3 "2022-03-02T12:01:59Z")

</div>

```julia
outer_dic[1].inner

```

?

---

<div class="post-metadata">

### Author: ![Christian\_Brunbjerg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christian_brunbjerg/32/34243_2.png) [@Christian\_Brunbjerg](https://discourse.julialang.org/u/Christian_Brunbjerg)
#### Post date: [March 2, 2022, 12:12pm UTC](https://discourse.julialang.org/t/extracting-elements-from-dictionary-containing-a-struct-where-the-struct-is-made-up-of-a-dictionary-containing-a-nested-struct/77288/4 "2022-03-02T12:12:11Z")

</div>

Thank you! It works like a charm!

---

<div class="post-metadata">

### Author: ![Kali](https://avatars.discourse-cdn.com/v4/letter/k/a88e57/32.png) [@Kali](https://discourse.julialang.org/u/Kali)
#### Post date: [March 2, 2022, 12:15pm UTC](https://discourse.julialang.org/t/extracting-elements-from-dictionary-containing-a-struct-where-the-struct-is-made-up-of-a-dictionary-containing-a-nested-struct/77288/5 "2022-03-02T12:15:39Z")

</div>

That only extracts a vector of the inner structs, and not the actual values inside the inner structs. But I believe that what goerch suggested works 🙂

---

<div class="post-metadata">

### Author: ![Kali](https://avatars.discourse-cdn.com/v4/letter/k/a88e57/32.png) [@Kali](https://discourse.julialang.org/u/Kali)
#### Post date: [March 2, 2022, 12:24pm UTC](https://discourse.julialang.org/t/extracting-elements-from-dictionary-containing-a-struct-where-the-struct-is-made-up-of-a-dictionary-containing-a-nested-struct/77288/7 "2022-03-02T12:24:02Z")

</div>

```julia
collect(outer_dic[1].inner[i].value for i in 1:3)

```

The above also works and is a pretty clean solution I think!

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [March 2, 2022, 1:48pm UTC](https://discourse.julialang.org/t/extracting-elements-from-dictionary-containing-a-struct-where-the-struct-is-made-up-of-a-dictionary-containing-a-nested-struct/77288/8 "2022-03-02T13:48:24Z")

</div>

```julia
getfiled.(od[1].inner,:value)

```

?
