# How to add values in a dictionary of lists?

**URL:** https://discourse.julialang.org/t/how-to-add-values-in-a-dictionary-of-lists/74192
**Category:** General Usage
**Tags:** question
**Created:** [January 7, 2022, 1:30pm UTC](https://discourse.julialang.org/t/how-to-add-values-in-a-dictionary-of-lists/74192 "2022-01-07T13:30:57Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [January 7, 2022, 1:30pm UTC](https://discourse.julialang.org/t/how-to-add-values-in-a-dictionary-of-lists/74192/1 "2022-01-07T13:30:57Z")

</div>

Hello, good morning.  
How to add values in a dictionary that has type (int, vector(int))? Cant understand the error about conversion

```julia
ERROR: LoadError: MethodError: Cannot `convert` an object of type Int64 to an object of type Vector{Tuple{Int64}}

```

```julia
using StatsBase
using Plots
using Combinatorics
using DataStructures

Ω = Dict((i, j) => i+j for i in 1:6 for j in 1:6);

println(length(Ω))
println(Ω)

buffer = DefaultDict{Int64, Vector{Tuple{Int64}}}(0);

for ((i,j), value) in Ω 
    println("Key: i: $i, j: $j -> $value")
    push!(buffer[value], (i, j));
end

```

---

<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: [January 7, 2022, 1:49pm UTC](https://discourse.julialang.org/t/how-to-add-values-in-a-dictionary-of-lists/74192/2 "2022-01-07T13:49:10Z")

</div>

You have probably mixed up your types you really want a bit, but for your

```julia
buffer = DefaultDict{Int64, Vector{Tuple{Int64}}}(0);

```

the proper push! would be e.g.:

```julia
julia> value=1
1

julia> i=2
2

julia> buffer = DefaultDict{Int64, Vector{Tuple{Int64}}}(0);

julia> push!( buffer, value => [(i,)] )
DefaultDict{Int64, Vector{Tuple{Int64}}, Int64} with 1 entry:
  1 => [(2,)]

```

But my guess is, you want something else.  
I can just speculate but I think it helps, if you look what a Tuple{Int} really is:

```julia
julia> x=(1,)
(1,)

julia> typeof(x)
Tuple{Int64}

julia> y=(1,2)
(1, 2)

julia> typeof(y)
Tuple{Int64, Int64}

```

---

<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: [January 7, 2022, 1:55pm UTC](https://discourse.julialang.org/t/how-to-add-values-in-a-dictionary-of-lists/74192/3 "2022-01-07T13:55:17Z")

</div>

But perhaps you want to push into a Vector in the Dict, for this you first have to create the Vector, because

```julia
buffer[value]

```

isn’t defined in your case.  
First check if

```julia
buffer[value]

```

exists:

```julia
haskey(buffer,value)

```

and if not create the Vector first:

```julia
v=[(i,)]
push!( buffer, value => v )

```

And if it exists, the push! would be e.g.:

```julia
push!( buffer[value], (4,) )

```

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [January 7, 2022, 3:00pm UTC](https://discourse.julialang.org/t/how-to-add-values-in-a-dictionary-of-lists/74192/4 "2022-01-07T15:00:05Z")

</div>

Thats a bit confusing…

for this

```julia
for ((i,j), value) in Ω 
    println("Key: i: $i, j: $j -> $value")
    push!(buffer, value => [(i, j)]);
end

```

got this error

```julia
ERROR: LoadError: MethodError: Cannot `convert` an object of type 
  Tuple{Int64{},Int64} to an object of type 
  Tuple{Int64{}}

```

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [January 7, 2022, 3:03pm UTC](https://discourse.julialang.org/t/how-to-add-values-in-a-dictionary-of-lists/74192/5 "2022-01-07T15:03:12Z")

</div>

What I want is create a key and append in vector of this key, because this will work as a histogram, and I will search for all the values of the vector using only the key

dict[7] will give-me all tuples (i,J) that i+j sums 7

---

<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: [January 7, 2022, 3:08pm UTC](https://discourse.julialang.org/t/how-to-add-values-in-a-dictionary-of-lists/74192/6 "2022-01-07T15:08:02Z")

</div>

> [@jcbritobr](#):
>
> `push!(buffer, value => [(i, j)]);`

For this you need:

```julia-auto
buffer = DefaultDict{Int64, Vector{Tuple{Int64,Int64}}}(0);

```

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [January 7, 2022, 3:08pm UTC](https://discourse.julialang.org/t/how-to-add-values-in-a-dictionary-of-lists/74192/7 "2022-01-07T15:08:43Z")

</div>

Now, it worked as you said.

```julia
DefaultDict(5 => [(2, 3)], 12 => [(6, 6)], 8 => [(5, 3)], 6 => [(2, 4)], 11 => [(6, 5)], 9 => [(5, 4)], 3 => [(2, 1)], 7 => [(6, 1)], 4 => [(2, 2)], 2 => [(1, 1)], 10 => [(5, 5)])

```

The issue was in the type declaration:

```julia
buffer = DefaultDict{Int64, Vector{Tuple{Int64}}}(0);

```

changing it to

```julia
buffer = DefaultDict{Int64, Vector{Tuple{Int64, Int64}}}(0);

```

worked

---

<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: [January 7, 2022, 3:10pm UTC](https://discourse.julialang.org/t/how-to-add-values-in-a-dictionary-of-lists/74192/8 "2022-01-07T15:10:19Z")

</div>

But your code has still an issue.  
You will never have more than one tuple in any array, because you overwrite if a key already exist in your Dict. You have to check if a key already exist.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [January 7, 2022, 3:40pm UTC](https://discourse.julialang.org/t/how-to-add-values-in-a-dictionary-of-lists/74192/9 "2022-01-07T15:40:33Z")

</div>

Isn’t the point of using a `DefaultDict` that you don’t have to check if a key already exists?

I imagine this is how the original code was supposed to look, i.e. getting both the value type and the default constructor right.

```julia
using DataStructures

Ω = Dict((i, j) => i+j for i in 1:6 for j in 1:6);

println(length(Ω))
println(Ω)

buffer = DefaultDict{Int, Vector{Tuple{Int, Int}}}(Vector{Tuple{Int, Int}});

for ((i,j), value) in Ω 
    println("Key: i: $i, j: $j -> $value")
    push!(buffer[value], (i, j));
end

```

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [January 7, 2022, 3:46pm UTC](https://discourse.julialang.org/t/how-to-add-values-in-a-dictionary-of-lists/74192/10 "2022-01-07T15:46:55Z")

</div>

yep, this leads to better code, and the issue root cause was the type declaration.

---

<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: [January 7, 2022, 3:53pm UTC](https://discourse.julialang.org/t/how-to-add-values-in-a-dictionary-of-lists/74192/11 "2022-01-07T15:53:22Z")

</div>

Didn’t know this and I didn’t look into the docs 😉
