# Dictionary with loop and squared numbers

**URL:** <https://discourse.julialang.org/t/dictionary-with-loop-and-squared-numbers/39353>\
**Category:** New to Julia\
**Tags:** array, dictionary, loops\
**Created:** [May 12, 2020, 3:16pm UTC](https://discourse.julialang.org/t/dictionary-with-loop-and-squared-numbers/39353 "2020-05-12T15:16:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ancapistola](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@Ancapistola](https://discourse.julialang.org/u/Ancapistola)\
**Post date:** [May 12, 2020, 3:16pm UTC](https://discourse.julialang.org/t/dictionary-with-loop-and-squared-numbers/39353/1 "2020-05-12T15:16:22Z")

</div>

i’m learning Julia through [juliaacademy.com](http://juliaacademy.com) and i’m not getting past exercise 4.2.  
4.1 I got it easy, but I ended up stalling right away. I’ve tried everything but it didn’t work at all.  
if anyone can help me i will be very grateful.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/f/cfb75cb655f258397cc9e203ed0b290d09e00b12.png)

Exercises  
4.1 Loop over integers between 1 and 100 and print their squares.

4.2 Add to the code above a bit to create a dictionary, `squares` that holds integers and their squares as key, value pairs such that

```julia
squares[10] == 100
```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 12, 2020, 3:29pm UTC](https://discourse.julialang.org/t/dictionary-with-loop-and-squared-numbers/39353/2 "2020-05-12T15:29:06Z")

</div>

What hav eyou tried? What hasn’t worked? Have you read the [manual section on Dictionaries](https://docs.julialang.org/en/v1/base/collections/#Dictionaries-1)?

---

<div class="post-metadata">

**Author:** ![Ancapistola](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@Ancapistola](https://discourse.julialang.org/u/Ancapistola)\
**Post date:** [May 12, 2020, 4:12pm UTC](https://discourse.julialang.org/t/dictionary-with-loop-and-squared-numbers/39353/3 "2020-05-12T16:12:01Z")

</div>

that was as close as I could get …

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/f/cfb75cb655f258397cc9e203ed0b290d09e00b12.png)

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [May 12, 2020, 4:14pm UTC](https://discourse.julialang.org/t/dictionary-with-loop-and-squared-numbers/39353/4 "2020-05-12T16:14:14Z")

</div>

Please don’t post screenshots - they’re hard to read. Instead, use [this guidance](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/5) to help you format your posts here.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 12, 2020, 4:15pm UTC](https://discourse.julialang.org/t/dictionary-with-loop-and-squared-numbers/39353/5 "2020-05-12T16:15:19Z")

</div>

You are creating a new `Dict` in every loop iteration - so you basically end up with a hundred dictionaries with one entry each.

What you want is to instantiate a `Dict` and then simply add key-value pairs to the existing single dictionary to end up with one `Dict` that has a hundred entries.

Have you looked at the manual section I linked above? You might be particularly interested in the line that references creating dictionaries from comprehensions 😉

---

<div class="post-metadata">

**Author:** ![Ancapistola](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@Ancapistola](https://discourse.julialang.org/u/Ancapistola)\
**Post date:** [May 12, 2020, 4:18pm UTC](https://discourse.julialang.org/t/dictionary-with-loop-and-squared-numbers/39353/6 "2020-05-12T16:18:59Z")

</div>

Thanks! 😉 👍
