# Pushing values to dic

**URL:** <https://discourse.julialang.org/t/pushing-values-to-dic/28537>\
**Category:** New to Julia\
**Created:** [September 8, 2019, 11:23am UTC](https://discourse.julialang.org/t/pushing-values-to-dic/28537 "2019-09-08T11:23:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [September 8, 2019, 11:23am UTC](https://discourse.julialang.org/t/pushing-values-to-dic/28537/1 "2019-09-08T11:23:55Z")

</div>

I’ve the below code:

```julia
dict = Dict{Int64, Vector{Float64}}()

files = filter(x->endswith(x, ".pdf"), readdir())
for (i, f) in enumerate(files)
       push!(dict[i], f)
end

```

But I got the below error:

```bash
KeyError: key 5 not found

Stacktrace:
 [1] getindex(::Dict{Int64,Array{Float64,1}}, ::Int64) at .\dict.jl:477
 [2] top-level scope at .\In[16]:21

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 8, 2019, 11:32am UTC](https://discourse.julialang.org/t/pushing-values-to-dic/28537/2 "2019-09-08T11:32:49Z")

</div>

The dictionary is empty, which is why `dict[5]` says that no such key exists. You probably want `dict[i] = [f] ` instead, that way a new array is assigned there.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [September 8, 2019, 11:37am UTC](https://discourse.julialang.org/t/pushing-values-to-dic/28537/3 "2019-09-08T11:37:32Z")

</div>

Alternatively, you can push a key-value pair:

`push!(dict, i => f)`

Personally, I’d prefer @Sukera’s answer though.

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [September 8, 2019, 11:37am UTC](https://discourse.julialang.org/t/pushing-values-to-dic/28537/4 "2019-09-08T11:37:38Z")

</div>

> [@Sukera](#):
>
> dict[i] = [f]

Thanks, I thought it is managed the same way of `Array`

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [September 8, 2019, 11:38am UTC](https://discourse.julialang.org/t/pushing-values-to-dic/28537/5 "2019-09-08T11:38:46Z")

</div>

> [@hasanOryx](#):
>
> Thanks, I thought it is managed the same way of `Array`

Your code wouldn’t work for an array either.

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [September 8, 2019, 11:40am UTC](https://discourse.julialang.org/t/pushing-values-to-dic/28537/6 "2019-09-08T11:40:32Z")

</div>

> [@carstenbauer](#):
>
> Your code wouldn’t work for an array either

mmm, it worked for this one:

```julia
found_skills = []
for (i, entry) in enumerate(found)
      push!(found_skills, entry.match)
end

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 8, 2019, 11:48am UTC](https://discourse.julialang.org/t/pushing-values-to-dic/28537/7 "2019-09-08T11:48:51Z")

</div>

> [@hasanOryx](#):
>
> `push!(found_skills, entry.match)`

This is not the code you had for `Dict`

You had

> [@hasanOryx](#):
>
> `push!(dict[i], f)`

If you actually treat the `Dict` as a collection and want to push to it though, it should work.

> [@carstenbauer](#):
>
> `push!(dict, i => f)`

Also, the question is what do you want to push in. Did you want an array of file names stored as value in the dict (i.e. `[f]`) or do you want to store the fiile name itself. Your initialization of the `dict` doesn’t match any of these.

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [September 8, 2019, 11:54am UTC](https://discourse.julialang.org/t/pushing-values-to-dic/28537/8 "2019-09-08T11:54:02Z")

</div>

Thanks, your answer made it clear for me.
