# CICLE FOR and DICTIONARY

**URL:** https://discourse.julialang.org/t/cicle-for-and-dictionary/30614
**Category:** General Usage
**Created:** [November 2, 2019, 9:50am UTC](https://discourse.julialang.org/t/cicle-for-and-dictionary/30614 "2019-11-02T09:50:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Caterina\_Cerutti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caterina_cerutti/32/11096_2.png) [@Caterina\_Cerutti](https://discourse.julialang.org/u/Caterina_Cerutti)
#### Post date: [November 2, 2019, 9:50am UTC](https://discourse.julialang.org/t/cicle-for-and-dictionary/30614/1 "2019-11-02T09:50:06Z")

</div>

I have to write a FOR’s cicle from 1 to 100 and then I have to create a “Dict” ,Called _squares_, which contains as key the int number and as elements their squares.

I have written

For i in 1:100  
Squares=Dict(i=\>i^2)  
End

But it doesn’t run.  
Can someone help me?

---

<div class="post-metadata">

### Author: ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)
#### Post date: [November 2, 2019, 10:08am UTC](https://discourse.julialang.org/t/cicle-for-and-dictionary/30614/2 "2019-11-02T10:08:34Z")

</div>

You’re close:

```julia
julia> squares = Dict() # but Dict{Int,Int}() is better when you need performance
Dict{Any,Any} with 0 entries

julia> for i=1:100
       squares[i] = i^2
       end

julia> squares 
Dict{Any,Any} with 100 entries:
  68 => 4624
  2 => 4
  89 => 7921
  11 => 121
  39 => 1521
  46 => 2116
  ...

```

Or here’s a nice one-liner:

```julia
julia> squares = Dict(i => i^2 for i in 1:100)
Dict{Int64,Int64} with 100 entries:
  68 => 4624
  2 => 4
  89 => 7921
  11 => 121
  39 => 1521
  46 => 2116
  ...

```

Note that elements are stored in indeterminate order in a `Dict`.

The code you wrote didn’t work because you assign a new `Dict` in each iteration of the loop. You should also avoid capitalizing keywords, but maybe that was just in your post.

---

<div class="post-metadata">

### Author: ![Caterina\_Cerutti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caterina_cerutti/32/11096_2.png) [@Caterina\_Cerutti](https://discourse.julialang.org/u/Caterina_Cerutti)
#### Post date: [November 2, 2019, 10:18am UTC](https://discourse.julialang.org/t/cicle-for-and-dictionary/30614/3 "2019-11-02T10:18:55Z")

</div>

Thank you very much!
