# Initialize Dict to enable values of different type

**URL:** https://discourse.julialang.org/t/initialize-dict-to-enable-values-of-different-type/91153
**Category:** General Usage
**Tags:** dictionary
**Created:** [December 2, 2022, 5:25pm UTC](https://discourse.julialang.org/t/initialize-dict-to-enable-values-of-different-type/91153 "2022-12-02T17:25:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [December 2, 2022, 5:25pm UTC](https://discourse.julialang.org/t/initialize-dict-to-enable-values-of-different-type/91153/1 "2022-12-02T17:25:51Z")

</div>

What is the reason that case 3 fails? And how to avoid this?  
How can I initialize a Dict to enable values of different type?  
Here is my snippet:

```julia

function MyDictFill(_a::Dict = Dict())
    if ~haskey(_a, "a")
        _a["a"] = true
    end
    if ~haskey(_a, "b")
        _a["b"] = 1.0
    end
    if ~haskey(_a, "c")
        _a["c"] = "str"
    end
    return _a
end

# case 1: ok
MyDictFill()

# case 2: ok
begin
    dict_a = Dict()
    dict_a["d"] = "star"
    dict_a["e"] = 1
    dict_a["f"] = true
    MyDictFill(dict_a)
end

# case 3: fails
begin
    dict_b = Dict()
    dict_b = Dict("d" => 2)
    dict_b = Dict("f" => "julia")
    MyDictFill(dict_b)
end

```

---

<div class="post-metadata">

### Author: ![p-gw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/p-gw/32/210518_2.png) [@p-gw](https://discourse.julialang.org/u/p-gw)
#### Post date: [December 2, 2022, 5:33pm UTC](https://discourse.julialang.org/t/initialize-dict-to-enable-values-of-different-type/91153/2 "2022-12-02T17:33:33Z")

</div>

What behaviour do you expect exactly?

The third case fails because you define `dict_b` 3 times.  
The last call gives you

```julia
julia> dict_b = Dict("f" => "julia")
Dict{String, String} with 1 entry:
  "f" => "julia"

```

If you fill a `Dict{String, String}` with non-string values, you will get a `MethodError`.

You could initialize the Dict with value type `Any` instead.

```julia
julia> dict_b = Dict{String,Any}("f" => "julia")
Dict{String, Any} with 1 entry:
  "f" => "julia"

julia> MyDictFill(dict_b)
Dict{String, Any} with 4 entries:
  "f" => "julia"
  "c" => "str"
  "b" => 1.0
  "a" => true

```

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [December 2, 2022, 5:37pm UTC](https://discourse.julialang.org/t/initialize-dict-to-enable-values-of-different-type/91153/3 "2022-12-02T17:37:25Z")

</div>

> [@p-gw](#):
>
> What behaviour do you expect exactly?

Thanks, now I see my mistake. Thanks a lot! 🙂

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [December 3, 2022, 9:53am UTC](https://discourse.julialang.org/t/initialize-dict-to-enable-values-of-different-type/91153/4 "2022-12-03T09:53:44Z")

</div>

Here some other examples of Dict initialization:

```julia
dict_b = Dict{String, Union{Nothing, String, Bool, Number}}("a" => "Julia", "b" => true, "c" => 10.0, "n" => nothing)
isnothing(dict_b["n"])

dict_c = Dict{Bool, String}(true => "Julia", false => "Romeo")
dict_c[true]

dict_d = Dict{Int, String}(1 => "one", 2 => "two", 3 => "three")
dict_d[1]
for (_k, _v) in dict_d
   println("#", _k, ": ", _v)
end
for _i = 1:length(dict_d) # eachindex() does not work here
   println("#", _i, ": ", dict_d[_i])
end

```
