# How to append multiple key-value pairs to an existing Dict

**URL:** https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676
**Category:** New to Julia
**Tags:** dictionary
**Created:** [August 2, 2021, 12:28pm UTC](https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676 "2021-08-02T12:28:29Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)
#### Post date: [August 2, 2021, 12:28pm UTC](https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676/1 "2021-08-02T12:28:29Z")

</div>

We can create a `Dict` from an iteratable of key-value pairs, e.g.,

```julia
julia> x = ['A', 'B', 'C']; y = [1, 2, 3];

julia> d = Dict(x .=> y)
Dict{Char, Int64} with 3 entries:
  'C' => 3
  'A' => 1
  'B' => 2

```

Now supposing I have another two collections of keys and values `x2` and `y2`, how can I insert them into the above `d` using a similar syntax?

```julia
julia> x2 = ['D', 'E', 'F']; y2 = [4, 5, 6];

julia> append!(d, x2 .=> y2)
ERROR: MethodError: no method matching append!(::Dict{Char, Int64}, ::Vector{Pair{Char, Int64}})
Closest candidates are:

julia> push!(d, x2 .=> y2)
ERROR: MethodError: no method matching push!(::Dict{Char, Int64}, ::Vector{Pair{Char, Int64}})

```

Do I have to use a loop to insert them one by one? 😂

---

<div class="post-metadata">

### Author: ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)
#### Post date: [August 2, 2021, 12:34pm UTC](https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676/2 "2021-08-02T12:34:42Z")

</div>

There are maybe better ways, but until then you can `merge()` multiple `Dict`s.

```julia
julia> x = ['A', 'B', 'C']; y = [1, 2, 3];

julia> d = Dict(x .=> y)
Dict{Char, Int64} with 3 entries:
  'C' => 3
  'A' => 1
  'B' => 2

julia> x2 = ['D', 'E', 'F']; y2 = [4, 5, 6];

julia> merge(d, Dict(x2 .=> y2))
Dict{Char, Int64} with 6 entries:
  'C' => 3
  'D' => 4
  'A' => 1
  'E' => 5
  'F' => 6
  'B' => 2

```

---

<div class="post-metadata">

### Author: ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)
#### Post date: [August 2, 2021, 12:41pm UTC](https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676/3 "2021-08-02T12:41:48Z")

</div>

Thanks. It is indeed a workaround. However, I am expecting something without creating a temporary `Dict`.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 2, 2021, 12:52pm UTC](https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676/4 "2021-08-02T12:52:57Z")

</div>

See related issue [here](https://github.com/JuliaLang/julia/issues/25904) and [Dictionaries.jl](https://github.com/andyferris/Dictionaries.jl)

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [August 2, 2021, 1:01pm UTC](https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676/5 "2021-08-02T13:01:46Z")

</div>

You can just broadcast `setindex!`:

```julia
julia> d = Dict(x .=> y)
Dict{Char, Int64} with 3 entries:
  'C' => 3
  'A' => 1
  'B' => 2

julia> setindex!.(Ref(d), y2, x2);

julia> d
Dict{Char, Int64} with 6 entries:
  'C' => 3
  'D' => 4
  'A' => 1
  'E' => 5
  'F' => 6
  'B' => 2

```

or even `push!.(Ref(d), x2 .=> y2)`.

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [August 2, 2021, 1:10pm UTC](https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676/6 "2021-08-02T13:10:46Z")

</div>

Loops can be short

```julia
foreach(((k, v), ) -> d[k] = v, zip(x2, y2))

```

`foreach` is good in this case, since it doesn’t produce extra allocations (like broadcasting does).

---

<div class="post-metadata">

### Author: ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)
#### Post date: [August 2, 2021, 1:16pm UTC](https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676/7 "2021-08-02T13:16:48Z")

</div>

> [@pfitzseb](#):
>
> You can just broadcast `setindex!` :

It is interesting. `setindex!.` applies broadcasting and returns a vector of 3 `Dict`s instead, though the three are essentially the same object.

```julia
julia> res = setindex!.(Ref(d), y2, x2)
3-element Vector{Dict{Char, Int64}}:
 Dict('C' => 3, 'D' => 4, 'A' => 1, 'E' => 5, 'F' => 6, 'B' => 2)
 Dict('C' => 3, 'D' => 4, 'A' => 1, 'E' => 5, 'F' => 6, 'B' => 2)
 Dict('C' => 3, 'D' => 4, 'A' => 1, 'E' => 5, 'F' => 6, 'B' => 2)

julia> res[1] === res[2] === res[3] === d
true

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 2, 2021, 1:17pm UTC](https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676/8 "2021-08-02T13:17:15Z")

</div>

Comprehension seems to be even shorter:

```julia
[d[x]=y for (x,y) in zip(x2,y2)]

```

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [August 2, 2021, 1:17pm UTC](https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676/9 "2021-08-02T13:17:46Z")

</div>

But allocates, which the `foreach` solution doesn’t.

---

<div class="post-metadata">

### Author: ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)
#### Post date: [August 2, 2021, 1:42pm UTC](https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676/10 "2021-08-02T13:42:14Z")

</div>

Sometimes I worry the answers we the community give in response to questions like this are a bit hard to read. E.g. there’s something surgically efficient about `foreach(((k, v), ) -> d[k] = v, zip(x2, y2))`, but it’s pretty hard to parse: nested tuples in an anonymous function, etc.

If I were a new-ish user and asked how to do something that seems like it should be simple and got advice about fancy higher order functions and avoiding allocation, I would probably be put off. Even as an intermediate user, it feels somehow wrong that the right way to add multiple items to a dictionary requires broadcasting over a `Ref` or an anonymous function in a `foreach` or something like that.

Anyway, my not-high-powered answer to the original question is to not fear the loop: loops are typically fast in Julia, and it’s not lot of characters. You can even do it in one line if you want:

```julia
for (k,v) in zip(x2, y2) d[k] = v end

```

(though in my opinion it’s more readable with line breaks)  
Shuhua, is there a particular reason you don’t want to loop?

---

<div class="post-metadata">

### Author: ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)
#### Post date: [August 2, 2021, 2:06pm UTC](https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676/11 "2021-08-02T14:06:11Z")

</div>

> [@evanfields](#):
>
> is there a particular reason you don’t want to loop?

Certainly no. 😃 I just want to know whether there are more convenient (built-in) functions like `push!` and `append!`.

Anyway, I have learned some _advanced_ techniques in all these answers. Thanks.  
For now, I think the _for loop_ is the best solution: intuitive and fast. (Also, no need to put the loop in one line just to make it look shorter 😝)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 3, 2021, 9:00am UTC](https://discourse.julialang.org/t/how-to-append-multiple-key-value-pairs-to-an-existing-dict/65676/12 "2021-08-03T09:00:57Z")

</div>

> [@evanfields](#):
>
> feels somehow wrong that the right way to add multiple items to a dictionary requires broadcasting over a `Ref` or an anonymous function in a `foreach` or something like that

I think it is great to see multiple solutions to a problem.

If they contain unfamiliar building blocks, this may be utilized as an opportunity to learn about them.
