# Cannot reassign dict entry with nothing value

**URL:** https://discourse.julialang.org/t/cannot-reassign-dict-entry-with-nothing-value/32321
**Category:** New to Julia
**Created:** [December 16, 2019, 7:53am UTC](https://discourse.julialang.org/t/cannot-reassign-dict-entry-with-nothing-value/32321 "2019-12-16T07:53:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![data\_dictator](https://avatars.discourse-cdn.com/v4/letter/d/7ba0ec/32.png) [@data\_dictator](https://discourse.julialang.org/u/data_dictator)
#### Post date: [December 16, 2019, 7:53am UTC](https://discourse.julialang.org/t/cannot-reassign-dict-entry-with-nothing-value/32321/1 "2019-12-16T07:53:58Z")

</div>

I just started coding in Julia today, and I have a slight problem with Dict(), when I create one that has only “nothing” or “missing” as values, I cannot later reassign the values to something else:

In: noth\_dict = Dict(“A” =\> nothing)  
Out: Dict{String,Nothing} with 1 entry:  
“A” =\> nothing

**In** : noth\_dict[“A”] = 1  
**Out:** MethodError: convert(::Type{Union{}}, ::Int64) is ambiguous. Candidates:  
convert(::Type{Union{}}, x) in Base at essentials.jl:166  
convert(::Type{T}, x::Number) where T\<:Number in Base at number.jl:7  
convert(::Type{T}, arg) where T\<:VecElement in Base at baseext.jl:8  
convert(::Type{T}, x::Number) where T\<:AbstractChar in Base at char.jl:179  
Possible fix, define  
convert(::Type{Union{}}, ::Number)

Stacktrace:  
[1] convert(::Type{Nothing}, ::Int64) at ./some.jl:34  
[2] setindex!(::Dict{String,Nothing}, ::Int64, ::String) at ./dict.jl:380  
[3] top-level scope at In[210]:1

I would appreciate any help with this.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [December 16, 2019, 7:58am UTC](https://discourse.julialang.org/t/cannot-reassign-dict-entry-with-nothing-value/32321/2 "2019-12-16T07:58:54Z")

</div>

Your dictionary can only hold keys of type `String` and values of type `Nothing`, as indicated in the output; it is a dictionary of type `Dict{String,Nothing}`. If you want your dictionary to accept, for example, both `nothing` and integers as values you can specify that when constructing the dictionary:

```julia
julia> dict = Dict{String,Union{Nothing,Int}}("A" => nothing)
Dict{String,Union{Nothing, Int64}} with 1 entry:
  "A" => nothing

julia> dict["A"] = 1
1

```

See also [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530) for how to post code-formatted code (which makes it easier to read).

---

<div class="post-metadata">

### Author: ![data\_dictator](https://avatars.discourse-cdn.com/v4/letter/d/7ba0ec/32.png) [@data\_dictator](https://discourse.julialang.org/u/data_dictator)
#### Post date: [December 16, 2019, 8:02am UTC](https://discourse.julialang.org/t/cannot-reassign-dict-entry-with-nothing-value/32321/3 "2019-12-16T08:02:49Z")

</div>

Thank you very much for the quick response! Now it makes sense, I was treating the Dict like a Python one. I suppose the type specifications is to allow for some optimisation. I will follow the post guidelines 🙂

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [December 16, 2019, 8:05am UTC](https://discourse.julialang.org/t/cannot-reassign-dict-entry-with-nothing-value/32321/4 "2019-12-16T08:05:01Z")

</div>

> [@data\_dictator](#):
>
> I was treating the Dict like a Python one

I believe a python dict would be equivalent to `Dict{Any,Any}` in Julia, i.e. it can hold any type of keys and values.
