# Adding elements to dictionaries?

**URL:** https://discourse.julialang.org/t/adding-elements-to-dictionaries/49670
**Category:** New to Julia
**Tags:** dictionary
**Created:** [November 6, 2020, 8:17am UTC](https://discourse.julialang.org/t/adding-elements-to-dictionaries/49670 "2020-11-06T08:17:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [November 6, 2020, 8:17am UTC](https://discourse.julialang.org/t/adding-elements-to-dictionaries/49670/1 "2020-11-06T08:17:49Z")

</div>

Suppose I have a dictionary `d = Dict("a"=->(1,2,3))` and I want to add an element with key `"b"` to the dictionary. The following works:

```julia
merge!(d,Dict("b"=>(4,5,6)))

```

However, this only seems to work if the value of the key has the same “type”. In other words, the following _fails_:

```julia
merge!(d, Dict("b"=>(7,9)))

```

because the value tuple has 2 elements instead of 3.

On the other hand, creating both elements at the same time works:

```julia
d = Dict("a"=>(1,2,3), "b"=>(7,9))

```

**Question** : is there/what is a more flexible method to add elements to a dictionary than the `merge!` method?

---

<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: [November 6, 2020, 8:40am UTC](https://discourse.julialang.org/t/adding-elements-to-dictionaries/49670/2 "2020-11-06T08:40:24Z")

</div>

You have to construct the original dictionary `d` with this flexibility in mind, for example

```julia
d = Dict{String,Any}("a"=->(1,2,3))

```

would allow values of type `String` as dictionary keys and `Any`thing as dictionary values.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [November 6, 2020, 8:43am UTC](https://discourse.julialang.org/t/adding-elements-to-dictionaries/49670/3 "2020-11-06T08:43:22Z")

</div>

> [@BLI](#):
>
> d = Dict(“a”=\>(1,2,3))

creates a specific dictionary where keys are `String` and values are exactly `Tuple{Int64,Int64,Int64}}`, thats why you get an error if you try to add a value which is `Tuple{Int64,Int64}}`.

The line

> [@BLI](#):
>
> `d = Dict("a"=>(1,2,3), "b"=>(7,9))`

gives you the hint on how you could do it:

```julia
julia> d = Dict("a"=>(1,2,3), "b"=>(7,9))
Dict{String,Tuple{Int64,Int64,Vararg{Int64,N} where N}} with 2 entries:
  "b" => (7, 9)
  "a" => (1, 2, 3)

```

See the type definition printed out. With that you can define the Dict like:

```julia
d=Dict{String,Tuple{Int64,Vararg{Int64,N} where N}}()

```

Which accepts any tuple:

```julia
julia> d["a"]=(1,2,3)
(1, 2, 3)

julia> d["b"]=(7,9)
(7, 9)

```

This is the way to assign new key,value pairs, no need for `merge!`.  
Or (as ninja’d above 🙂 ) use `Any`.

But use the correct syntax: not `=->` , but `=>`
