# Dictionary Initialization and Usage

**URL:** <https://discourse.julialang.org/t/dictionary-initialization-and-usage/74609>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 14, 2022, 11:57am UTC](https://discourse.julialang.org/t/dictionary-initialization-and-usage/74609 "2022-01-14T11:57:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajat\_Chaudhari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajat_chaudhari/32/32783_2.png) [@Rajat\_Chaudhari](https://discourse.julialang.org/u/Rajat_Chaudhari)\
**Post date:** [January 14, 2022, 11:57am UTC](https://discourse.julialang.org/t/dictionary-initialization-and-usage/74609/1 "2022-01-14T11:57:27Z")

</div>

I started with Julia today and was experimenting with Dictionary feature. The following is the code I wrote when I faced the error. Could anyone point out as to why such error is occuring and why such usage is wrong?

 ![error_1](https://global.discourse-cdn.com/julialang/original/3X/f/c/fcd5da38f8fbcf1aa9b255a77f3d2c7eb5f36721.png)

Any help would be appreciated!

---

<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:** [January 14, 2022, 12:07pm UTC](https://discourse.julialang.org/t/dictionary-initialization-and-usage/74609/3 "2022-01-14T12:07:32Z")

</div>

Maybe Julia thinks it is inconceivable that Freiburg is third??

The actual answer is:

```julia
julia> x = Dict{String, Int64}
Dict{String, Int64}

julia> typeof(x) # x is not a Dict!
DataType

```

you have not instantiated a Dictionary, but rather assigned the value `Dict{String, Int64}`, which is a type, to the binding `bundesliga_table_position`.

The error is telling you that you cannot add an entry to a `DataType`. To instantiate an object of a type, use parentheses:

```julia
julia> x = Dict{String, Int64}()
Dict{String, Int64}()

julia> typeof(x) # x is a Dict now!
Dict{String, Int64}

julia> x["a"] = 1
1

julia> x
Dict{String, Int64} with 1 entry:
  "a" => 1

```

---

<div class="post-metadata">

**Author:** ![Rajat\_Chaudhari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajat_chaudhari/32/32783_2.png) [@Rajat\_Chaudhari](https://discourse.julialang.org/u/Rajat_Chaudhari)\
**Post date:** [January 14, 2022, 12:14pm UTC](https://discourse.julialang.org/t/dictionary-initialization-and-usage/74609/4 "2022-01-14T12:14:58Z")

</div>

Although inconceivable for sure 😉 your answer is spot on. I didn’t think this was even possible!  
Thank you for your crisp and prompt answer. Thanks for the help!

---

<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:** [January 14, 2022, 12:16pm UTC](https://discourse.julialang.org/t/dictionary-initialization-and-usage/74609/5 "2022-01-14T12:16:10Z")

</div>

No problem. One additional complication that I missed is that your first line also made `bundesliga_table_position` and alias for `Dict{String, Int64}` which can therefore be used to construct a dictionary. So you’ve essentially created a less flexible `Dict` constructor:

```julia
julia> my_alias = Dict{String, Int64}
Dict{String, Int64}

julia> Dict("a" => 1, "b" => 2)
Dict{String, Int64} with 2 entries:
  "b" => 2
  "a" => 1

julia> my_alias("a" => 1, "b" => 2) # my_alias can now create new Dicts
Dict{String, Int64} with 2 entries:
  "b" => 2
  "a" => 1

julia> my_alias("a" => "b", "b" => "c") # But only String => Int Dicts
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Int64

julia> Dict("a" => "b", "b" => "c") # Unlike the actual Dict constructor
Dict{String, String} with 2 entries:
  "b" => "c"
  "a" => "b"

```

---

<div class="post-metadata">

**Author:** ![Rajat\_Chaudhari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajat_chaudhari/32/32783_2.png) [@Rajat\_Chaudhari](https://discourse.julialang.org/u/Rajat_Chaudhari)\
**Post date:** [January 14, 2022, 12:23pm UTC](https://discourse.julialang.org/t/dictionary-initialization-and-usage/74609/6 "2022-01-14T12:23:09Z")

</div>

Ohh yes. That makes sense. 👍
