# Loop in dictionary definition

**URL:** https://discourse.julialang.org/t/loop-in-dictionary-definition/63951
**Category:** New to Julia
**Created:** [July 2, 2021, 5:40pm UTC](https://discourse.julialang.org/t/loop-in-dictionary-definition/63951 "2021-07-02T17:40:44Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [July 2, 2021, 5:40pm UTC](https://discourse.julialang.org/t/loop-in-dictionary-definition/63951/1 "2021-07-02T17:40:44Z")

</div>

Hello,  
I’m using a library that uses a dictionary to set variables along points, like so:

```julia
bla = Dict(
    1 => foo,
    2 => bar
)

```

I want to create that dictionary over a large number of points and that number is not constant. Something like:

```julia
bla = Dict(
    for i in 1:N
        i => foo[i],
    end
)

```

How can I achieve this?  
Thanks a lot!

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [July 2, 2021, 5:46pm UTC](https://discourse.julialang.org/t/loop-in-dictionary-definition/63951/2 "2021-07-02T17:46:47Z")

</div>

You can use a comprehension for this::

```julia
julia> N = 5;

julia> foo = rand(N);

julia> bla = Dict(i => foo[i] for i in 1:N)
Dict{Int64, Float64} with 5 entries:
  5 => 0.825488
  4 => 0.308265
  2 => 0.932313
  3 => 0.495245
  1 => 0.490559

```

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [July 2, 2021, 5:52pm UTC](https://discourse.julialang.org/t/loop-in-dictionary-definition/63951/3 "2021-07-02T17:52:41Z")

</div>

> [@rdeits](#):
>
> You can use a comprehension for this:

Oh, that’s great, thanks!  
How do I combine that with a defined entry? I never worked with Dictionaries before. Can I add a new point (say point 6) after `bla = Dict(i => foo[i] for i in 1:N)`?  
Something like:

```julia
bla = Dict(i => foo[i] for i in 1:N, 6=>0.2)

```

Thanks!

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [July 2, 2021, 5:56pm UTC](https://discourse.julialang.org/t/loop-in-dictionary-definition/63951/4 "2021-07-02T17:56:15Z")

</div>

Just figured out the merge command. So if that’s the way to go, I’m happy. Thanks again!

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [July 2, 2021, 6:14pm UTC](https://discourse.julialang.org/t/loop-in-dictionary-definition/63951/5 "2021-07-02T18:14:35Z")

</div>

Yup, `merge` will certainly work. On the other hand, you might also consider just using a loop–loops are fast in Julia, and often the easiest way to do things:

```julia
julia> d = Dict{Int, Float64}()
Dict{Int64, Float64}()

julia> for i in 1:N
         d[i] = foo[i]
       end

julia> d[6] = 0.2
0.2

julia> d
Dict{Int64, Float64} with 6 entries:
  5 => 0.825488
  4 => 0.308265
  6 => 0.2
  2 => 0.932313
  3 => 0.495245
  1 => 0.490559

```

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [July 2, 2021, 6:26pm UTC](https://discourse.julialang.org/t/loop-in-dictionary-definition/63951/6 "2021-07-02T18:26:45Z")

</div>

Ah, that is also a neat way to do it. Thanks!
