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

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

``````# 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 [inlined]

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

``````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> 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> 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
``````
6 Likes
{"ft_lang_label":"__label__en","ft_lang_prob":0.6706558,"math_prob":0.9506031,"size":1651,"snap":"2023-40-2023-50","text_gpt3_token_len":622,"char_repetition_ratio":0.15907711,"word_repetition_ratio":0.23465703,"special_character_ratio":0.40096912,"punctuation_ratio":0.21750663,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98523784,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-25T13:33:35Z\",\"WARC-Record-ID\":\"<urn:uuid:0533f25b-105d-4a35-800b-d9e0d0893ac5>\",\"Content-Length\":\"26808\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:20b2cb5d-3e9d-43ba-a088-7b75476adebc>\",\"WARC-Concurrent-To\":\"<urn:uuid:6a2c4c60-9956-4c32-822f-8b0c565c5e6e>\",\"WARC-IP-Address\":\"184.105.99.43\",\"WARC-Target-URI\":\"https://discourse.julialang.org/t/why-i-am-not-to-update-string-as-value-in-dictionary-if-dictionary-is-created-with-int64-values/102952\",\"WARC-Payload-Digest\":\"sha1:E6VUTNPEYAHY7QTNTJRXVPSOR4P3IHH5\",\"WARC-Block-Digest\":\"sha1:CRNCBCEEYUHNCKVMLNL7CZ65PF6ZRTHC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233508977.50_warc_CC-MAIN-20230925115505-20230925145505-00278.warc.gz\"}"}