# Julia how to create a dictionary using a loop

**URL:** https://discourse.julialang.org/t/julia-how-to-create-a-dictionary-using-a-loop/39935
**Category:** General Usage
**Tags:** question
**Created:** [May 22, 2020, 5:17am UTC](https://discourse.julialang.org/t/julia-how-to-create-a-dictionary-using-a-loop/39935 "2020-05-22T05:17:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Vondoe79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vondoe79/32/11790_2.png) [@Vondoe79](https://discourse.julialang.org/u/Vondoe79)
#### Post date: [May 22, 2020, 5:17am UTC](https://discourse.julialang.org/t/julia-how-to-create-a-dictionary-using-a-loop/39935/1 "2020-05-22T05:17:40Z")

</div>

Hi there,

I am new to Julia and I need a little help with the abovementioned task. I want to create a list of dictionary with tuples as keys and a real number as values. Below is the piece of code a wrote, but the result I get,  
`(5, 5)=>3.0e-5`  
,seem to only show the last (key, value) pair.

```julia
v = 5
w=[]
for i=1:v
	for j=1:v
		if i != j
			w=(i,j) => 0.00001
		else
			w=(i,j) => 0.00003
		end
	end
end
println("w \n :-----> ", w)

```

What I was really expecting are the following:

```julia
# expected outcome:
[(1,1) => 0.00003, (1,2)=> 0.0001, (1,3) => 0.00001, (1,4) => 0.00001, (1,5) => 0.00001,
(2,1) => 0.00001, (2,2) => 0.00003, (2,3) => 0.00001, (2,4) => 0.00001, .... , (5,5) => 0.00003]

```

Thanks in advance for your help.

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [May 22, 2020, 5:40am UTC](https://discourse.julialang.org/t/julia-how-to-create-a-dictionary-using-a-loop/39935/2 "2020-05-22T05:40:20Z")

</div>

This works:

```julia
julia> v = 5
       w = Dict()
       for i=1:v
           for j=1:v
               if i != j
                   w[(i,j)] = 0.00001
               else
                   w[(i,j)] = 0.00003
               end
           end
       end
       w
Dict{Any,Any} with 25 entries:
[...]

```

Alternatively,

```julia
julia> v = 5
       w = Dict(
           (i,j) => if i != j
               0.00001
           else 
               0.00003 
           end 
           for i = 1:v, j = 1:v
       )
Dict{Tuple{Int64,Int64},Float64} with 25 entries:
[...]

```

One advantage of the latter is that you get a dictionary with concrete types (`Dict{Tuple{Int64,Int64},Float64}`) rather than `Dict{Any,Any}`. Concrete types are much more performant.

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [May 22, 2020, 5:47am UTC](https://discourse.julialang.org/t/julia-how-to-create-a-dictionary-using-a-loop/39935/3 "2020-05-22T05:47:57Z")

</div>

```julia
v = 5
w = Pair{Tuple{Int64,Int64},Float64}[]
for i=1:v , j=1:v
		push!( w , (i,j) => i != j ? 0.00001 : 0.00003 )
end
println("w \n :-----> ", w)

```

Assuming that you wanted a list of pairs (rather than an actual dictionary)

---

<div class="post-metadata">

### Author: ![Vondoe79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vondoe79/32/11790_2.png) [@Vondoe79](https://discourse.julialang.org/u/Vondoe79)
#### Post date: [May 22, 2020, 1:12pm UTC](https://discourse.julialang.org/t/julia-how-to-create-a-dictionary-using-a-loop/39935/4 "2020-05-22T13:12:50Z")

</div>

Thanks a bunch! Your answer is very concise.
