# Array of Dict to array of String

**URL:** <https://discourse.julialang.org/t/array-of-dict-to-array-of-string/61979>\
**Category:** Data\
**Tags:** array, dictionary\
**Created:** [May 28, 2021, 8:10am UTC](https://discourse.julialang.org/t/array-of-dict-to-array-of-string/61979 "2021-05-28T08:10:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laco\_Kovac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laco_kovac/32/20684_2.png) [@Laco\_Kovac](https://discourse.julialang.org/u/Laco_Kovac)\
**Post date:** [May 28, 2021, 8:10am UTC](https://discourse.julialang.org/t/array-of-dict-to-array-of-string/61979/1 "2021-05-28T08:10:05Z")

</div>

Having array of Dict, e.g.

`x1 = [Dict("a"=>"a", "b"=>"b", "c"=>"c"), Dict("a"=>"e", "b"=>"f", "c"=>"g"), Dict("a"=>"h", "b"=>"i", "c"=>"j")]`

What would be the most efficient way to convert it to array of strings consisting only of values of Dict item “a”. (We can safely assume that each Dict in input array contains item “a”)

I mean to get a result (based on example above) like this:

`["a", "e", "h"]`

Thank you,

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [May 28, 2021, 8:44am UTC](https://discourse.julialang.org/t/array-of-dict-to-array-of-string/61979/2 "2021-05-28T08:44:52Z")

</div>

I think this should work, although I don’t know if it is _the_ most efficient way

```julia
things = [d["a"] for d in x1 if haskey(d, "a")]

```

(not actually tested)

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 28, 2021, 8:49am UTC](https://discourse.julialang.org/t/array-of-dict-to-array-of-string/61979/3 "2021-05-28T08:49:30Z")

</div>

Broadcasting seems slightly more efficient than `[d["a"] for d in x1]` from my testing:

```julia
getindex.(x1, "a")

```

---

<div class="post-metadata">

**Author:** ![Laco\_Kovac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laco_kovac/32/20684_2.png) [@Laco\_Kovac](https://discourse.julialang.org/u/Laco_Kovac)\
**Post date:** [May 28, 2021, 8:55am UTC](https://discourse.julialang.org/t/array-of-dict-to-array-of-string/61979/4 "2021-05-28T08:55:25Z")

</div>

> [@sijo](#):
>
> `getindex.(x1, "a")`

Thank you both. With for loop (1st solution) it takes for me 0.042684 seconds (69.95 k allocations: 3.495 MiB). With getindex. it takes 0.000015 seconds (4 allocations: 192 bytes).

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [May 28, 2021, 9:07am UTC](https://discourse.julialang.org/t/array-of-dict-to-array-of-string/61979/5 "2021-05-28T09:07:16Z")

</div>

Good catch. The only thing left to say is that broadcast fails when any of the Dicts does not contain “a”. As long as that’s not the case, broadcasting is the best way

Edit: nvm, I just reread the question and he did say that all dicts do contain “a” here

---

<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:** [May 29, 2021, 5:20pm UTC](https://discourse.julialang.org/t/array-of-dict-to-array-of-string/61979/6 "2021-05-29T17:20:53Z")

</div>

you could use also this

get.(x1,“a”,“NA”)
