# Why I am not to update String as value in Dictionary if Dictionary is created with Int64 values

**URL:** https://discourse.julialang.org/t/why-i-am-not-to-update-string-as-value-in-dictionary-if-dictionary-is-created-with-int64-values/102952
**Category:** General Usage
**Tags:** question
**Created:** [August 18, 2023, 12:47pm UTC](https://discourse.julialang.org/t/why-i-am-not-to-update-string-as-value-in-dictionary-if-dictionary-is-created-with-int64-values/102952 "2023-08-18T12:47:57Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![saampandit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saampandit/32/52335_2.png) [@saampandit](https://discourse.julialang.org/u/saampandit)
#### Post date: [August 18, 2023, 12:47pm UTC](https://discourse.julialang.org/t/why-i-am-not-to-update-string-as-value-in-dictionary-if-dictionary-is-created-with-int64-values/102952/1 "2023-08-18T12:47:57Z")

</div>

I am working and learning Dictionaries, now here is what is happening,

```Julia
# Initialize a dictionary
d = Dict("a"=>1, "b" => 2, "c" => 3)
d["c"] = 4 # works 
d["c"] = "Hello" # Fails with error pasted below
d["d"] = "Hello" # Fails with same error
d["d"] = 9 # works

```

Error is

> 

MethodError: Cannot `convert` an object of type String to an object of type Int64

Closest candidates are:

convert(::Type{T}, !Matched::T) where T\<:Number

@ Base number.jl:6

convert(::Type{T}, !Matched::Number) where T\<:Number

@ Base number.jl:7

convert(::Type{T}, !Matched::Base.TwicePrecision) where T\<:Number

@ Base twiceprecision.jl:273

…

1. **setindex!** (::Dict{String, Int64}, ::String, ::String)@_dict.jl:369_
2. **top-level scope** @_[Local: 1](http://localhost:1234/edit?id=cff03866-3db9-11ee-1e9b-434dbd6155c2#)_ [inlined]

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 18, 2023, 12:52pm UTC](https://discourse.julialang.org/t/why-i-am-not-to-update-string-as-value-in-dictionary-if-dictionary-is-created-with-int64-values/102952/2 "2023-08-18T12:52:40Z")

</div>

> [@saampandit](#):
>
> `d = Dict("a"=>1, "b" => 2, "c" => 3)`

When you initialize your dict like the above, the Dict created becomes specific for string keys and int values:

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

```

(note the `Dict{String, Int64}`).

If you want the Dict to contain more general values than those used to initialize the dict, you can be explicit about that, for example:

```julia
julia> d = Dict{String,Union{String,Int}}("a"=>1, "b" => 2, "c" => 3)
Dict{String, Union{Int64, String}} with 3 entries:
  "c" => 3
  "b" => 2
  "a" => 1

julia> d["c"] = "a"
"a"

julia> d
Dict{String, Union{Int64, String}} with 3 entries:
  "c" => "a"
  "b" => 2
  "a" => 1

```

or, even more general:

```julia
julia> d = Dict{String,Any}("a"=>1, "b" => 2, "c" => 3)
Dict{String, Any} with 3 entries:
  "c" => 3
  "b" => 2
  "a" => 1

julia> d["c"] = [1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> d
Dict{String, Any} with 3 entries:
  "c" => [1, 2, 3]
  "b" => 2
  "a" => 1

```
