# How using push on typed dictionary

**URL:** https://discourse.julialang.org/t/how-using-push-on-typed-dictionary/33827
**Category:** New to Julia
**Created:** [January 26, 2020, 11:15pm UTC](https://discourse.julialang.org/t/how-using-push-on-typed-dictionary/33827 "2020-01-26T23:15:44Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)
#### Post date: [January 26, 2020, 11:15pm UTC](https://discourse.julialang.org/t/how-using-push-on-typed-dictionary/33827/1 "2020-01-26T23:15:44Z")

</div>

Hello everybody, i was playing with dictionary … i can’t understand why the last my statement is not correct in this case:

```julia

println("1° dictionary")
myDictionary1 = Dict()
push!(myDictionary1,"alpha"=>[10,20]) # ok this works, using push

println("2° dictionary")
myDictionary2 = Dict{String,Vector{Int64}}
myDictionary2("betha"=>[1,2,3,4,5]) # another possible way to add to dictionary
println("2° dictionary using push")
push!(myDictionary2,"alpha"=>[10,20]) # why this gives an error???

```

I was trying to be more precise in type definition of my Dict, and i tried to transform Dict() in Dict{String,Vector{Int64}}, but, surprisingly, Julia tell me that the last line is not correct, also if in the exact sintex i used in the first dictionary. The error is:

```julia

ERROR: LoadError: MethodError: no method matching push!(::Type{Dict{String,Array{Int64,1}}}, ::Pair{String,Array{Int64,1}})
Closest candidates are:
  push!(::Any, ::Any, ::Any) at abstractarray.jl:2159
  push!(::Any, ::Any, ::Any, ::Any...) at abstractarray.jl:2160
  push!(::Array{Any,1}, ::Any) at array.jl:875
  ...
Stacktrace:
 [1] top-level scope at C:\vmswap\juliaPrj\querere\example01.jl:9
in expression starting at C:\vmswap\juliaPrj\querere\example01.jl:9

```

Thanks for help! i really don’t understand …

---

<div class="post-metadata">

### Author: ![Jakub\_Wronowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_wronowski/32/204030_2.png) [@Jakub\_Wronowski](https://discourse.julialang.org/u/Jakub_Wronowski)
#### Post date: [January 26, 2020, 11:40pm UTC](https://discourse.julialang.org/t/how-using-push-on-typed-dictionary/33827/2 "2020-01-26T23:40:30Z")

</div>

> [@Darione](#):
>
> myDictionary2 = Dict{String,Vector{Int64}}

There is `()` missing, you are trying to call `push!` on a type, not an instance.  
Error message is quite clear here I think. Any ideas to improve it?

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [January 26, 2020, 11:44pm UTC](https://discourse.julialang.org/t/how-using-push-on-typed-dictionary/33827/3 "2020-01-26T23:44:33Z")

</div>

> [@Jakub\_Wronowski](#):
>
> a type, not an instance.  
> Error message is quite clear here I think. Any ideas to improve it?

maybe defining an instance of` push!(a::AbstractDict,x::Any)` would help,other option is to add a verification when a function is called on a type instead of an instance of that type and print an adequate explanation. for example:

```julia
ERROR: LoadError: MethodError: no method matching push!(::Type{Dict{String,Array{Int64,1}}}, ::Pair{String,Array{Int64,1}})
Closest candidates are:
  push!(::Any, ::Any, ::Any) at abstractarray.jl:2159
  push!(::Any, ::Any, ::Any, ::Any...) at abstractarray.jl:2160
  push!(::Array{Any,1}, ::Any) at array.jl:875
  ...
Posible problems include:
  -The function was called with a type instead of an instance of said type.
Stacktrace:
 [1] top-level scope at C:\vmswap\juliaPrj\querere\example01.jl:9
in expression starting at C:\vmswap\juliaPrj\querere\example01.jl:9

```

---

<div class="post-metadata">

### Author: ![Jakub\_Wronowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_wronowski/32/204030_2.png) [@Jakub\_Wronowski](https://discourse.julialang.org/u/Jakub_Wronowski)
#### Post date: [January 26, 2020, 11:49pm UTC](https://discourse.julialang.org/t/how-using-push-on-typed-dictionary/33827/4 "2020-01-26T23:49:31Z")

</div>

Good idea, generating additional message in such case would be useful. Anyway, this case is really tricky, as this syntax looks like addition really works, but constructor was called instead. Quite funny case, thanks for sharing @Darione .

---

<div class="post-metadata">

### Author: ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)
#### Post date: [January 27, 2020, 12:03pm UTC](https://discourse.julialang.org/t/how-using-push-on-typed-dictionary/33827/5 "2020-01-27T12:03:45Z")

</div>

Hi all … I still do not understand well …

This works:

```julia
println("2° dictionary")
myDictionary2 = Dict{String,Vector{Int64}}
myDictionary2("betha"=>[1,2,3,4,5])

```

This other doesn’t:

```julia
println("3° dictionary")
myDictionary3 = Dict{String,Vector{Int64}}
push!(myDictionary3,"alpha"=>[10,20])     

```

How exactly should be the last line, using push! ? or is myDictionary3 = … that i have to change in another way, to have an instance and not a type ?

Many thanks 🙂

---

<div class="post-metadata">

### Author: ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)
#### Post date: [January 27, 2020, 12:21pm UTC](https://discourse.julialang.org/t/how-using-push-on-typed-dictionary/33827/6 "2020-01-27T12:21:24Z")

</div>

In the case of `myDictionary3`, you cannot push a key-value pair into a `Type`. You need to instantiate by calling the constructor, with`()` at the end:

```julia
myDictionary3 = Dict{String,Vector{Int64}}()

```

Without the `()`, `myDictionary3` is just an alias for `Dict{String,Vector{Int64}}`. A similar example would be `INT = Int64`. Here I simply created the alias `INT` for the `Int64` Type (it is not a variable containing some Int value). This means that if I wanted to convert a float to int I could use `INT(2.0)` instead of `Int64(2.0)`, and it would work the same.

In the case of `myDictionary2`, you call the constructor directly with the line `myDictionary2("betha"=>[1,2,3,4,5])`

See:  
[Introducing Julia/Dictionaries and sets - Wikibooks, open books for an open world](https://en.wikibooks.org/wiki/Introducing_Julia/Dictionaries_and_sets), at " More complex structures" section

---

<div class="post-metadata">

### Author: ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)
#### Post date: [January 27, 2020, 2:03pm UTC](https://discourse.julialang.org/t/how-using-push-on-typed-dictionary/33827/7 "2020-01-27T14:03:30Z")

</div>

Ahhhhhh, now I understand, thank you very much @Iulian.Cioarca !  
I will read with attention the link you copy me.  
I rember to have read the type redefinition one time … i will read again.

---

<div class="post-metadata">

### Author: ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)
#### Post date: [January 27, 2020, 2:57pm UTC](https://discourse.julialang.org/t/how-using-push-on-typed-dictionary/33827/8 "2020-01-27T14:57:37Z")

</div>

Another question related to this.

If I want to be, as in that case, precise on the type of dictionary I need (writing Dict{String,Vector{Int64}}() instead of simply Dict() ) , it will get a benefit in terms of speed of execution of the code, or, in your opinion, it is irrelevant because, in short, Julia understands the type of dictionary I serve and do you fill in?

I tried in a for loop, where in every iteration I define a dictionary of tens of thousands of string keys, trying both the use of Dict () and the one with the precise type I need, and it seems to me that nothing changes in terms of speed of execution. It’s correct? or i should see a gain in speed if i define exacty the type?

Thank you very much for help to make me undestand the best way to use Julia.

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [February 3, 2020, 8:39pm UTC](https://discourse.julialang.org/t/how-using-push-on-typed-dictionary/33827/9 "2020-02-03T20:39:39Z")

</div>

I havent checked, but if you create a dict directly with the keys, the types are inferred automatically from the key value pairs, so it’s not necessary when you create dicts with keys inside.  
it’s relevant when you create empty dicts, as without keys, the diccionary does not have any information to create an appropriate structure, and defaults to the slow type `Any`
