# How to extract first arguments from dictionaries in Julia?

**URL:** https://discourse.julialang.org/t/how-to-extract-first-arguments-from-dictionaries-in-julia/103653
**Category:** New to Julia
**Created:** [September 8, 2023, 9:17am UTC](https://discourse.julialang.org/t/how-to-extract-first-arguments-from-dictionaries-in-julia/103653 "2023-09-08T09:17:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)
#### Post date: [September 8, 2023, 9:17am UTC](https://discourse.julialang.org/t/how-to-extract-first-arguments-from-dictionaries-in-julia/103653/1 "2023-09-08T09:17:48Z")

</div>

Hello,  
this is a basic question but I am not familiar with dictionaries, preferring arrays. I have set a dictionary as

```julia
julia> D = Dict("Field_1"=>[1,2,3,4,5], 
           "Field_2"=>["a", "b", "c", "d", "e"])
Dict{String, Vector} with 2 entries:
  "Field_1" => [1, 2, 3, 4, 5]
  "Field_2" => ["a", "b", "c", "d", "e"]

```

What is the function to assess how many entries are in it? That is, I should get a `n = 5` somehow?  
Also, it is possible to extract the first entry of each field and assign it to a different array?  
That is, if I reiterate between 1 and n, can I do something like:

```julia
d = ["Field_1" [1], "Field_2" [a]]
A = d[1]
B = d[2]

```

Thank you

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [September 8, 2023, 9:36am UTC](https://discourse.julialang.org/t/how-to-extract-first-arguments-from-dictionaries-in-julia/103653/2 "2023-09-08T09:36:21Z")

</div>

> [@Luigi\_Marongiu](#):
>
> What is the function to assess how many entries are in it? That is, I should get a `n = 5` somehow?

By “it” do you mean the arrays stored as the values in the dictionary? A dictionary is made up of `(key, value)` pairs:

```julia
julia> for (k, v) ∈ pairs(D)
           println("Key: ", k, " value: ", v)
       end
Key: Field_1 value: [1, 2, 3, 4, 5]
Key: Field_2 value: ["a", "b", "c", "d", "e"]

```

So if you want to check the number of entries in each array you can do:

```julia
julia> length.(values(D))
2-element Vector{Int64}:
 5
 5

```

I’m afraid I don’t understand the second question. The first element of each array in your dict is accessed in the same way as computing the length above:

```julia
julia> first.(values(D))
2-element Vector{Any}:
 1
  "a"

```

---

<div class="post-metadata">

### Author: ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)
#### Post date: [September 8, 2023, 9:36am UTC](https://discourse.julialang.org/t/how-to-extract-first-arguments-from-dictionaries-in-julia/103653/3 "2023-09-08T09:36:33Z")

</div>

You could use, e.g., `length(D["Field_1"])` to obtain the length of the array in Field\_1, and `first(D["Field_1"])` to obtain the first element.

---

<div class="post-metadata">

### Author: ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)
#### Post date: [September 8, 2023, 1:09pm UTC](https://discourse.julialang.org/t/how-to-extract-first-arguments-from-dictionaries-in-julia/103653/4 "2023-09-08T13:09:07Z")

</div>

> [@nilshg](#):
>
> ```julia
> for (k, v) ∈ pairs(D)
> println("Key: ", k, " value: ", v)
> end
> 
> ```

Thank you. `k` and `v` extract the data exactly as needed. `length` gives me also the information I needed.
