# Setting up Dict example puzzled by syntax and functionality

**URL:** <https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823>\
**Category:** New to Julia\
**Tags:** dictionary\
**Created:** [February 20, 2022, 10:08pm UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823 "2022-02-20T22:08:42Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [February 20, 2022, 10:08pm UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/1 "2022-02-20T22:08:42Z")

</div>

Hi all  
noob here. I am SLOWLY learning how to code properly in julia and trying NOT to bring my python baggage with me. I’ve set myself a high level goal and sub divided it into tasks. So I wanted to read a csv and put it into a Dict. I read up on Dicts, pretty standard stuff, and then thought to look for an example to reverse engineer. I found one and it contains the following code

the csv file

```julia
state,abbrev,fips
alabama,AL,01
alaska,AK,02
arizona,AZ,04

```

the code I found.

```julia
states_dict = Dict(
    "headers" => [Dict("text" => "Name", "value" => "name"), Dict("text" => "Abbreviation", "value" => "abbrev"), Dict("text" => "FIPS", "value" => "fips")],
    "states" => [Dict("name" => states[1][i,1], "abbrev" => states[1][i,2], "fips" => states[1][i,3]) for i in 1:size(states[1],1)]
)

```

I can see what it does but am confused about HOW to track down some aspects of the solution.

**I don’t understand the relationship of headers “name” to states “name” in this case?**

AND

how to track down how this works. I can see WHAT it does but can’t find anywhere that explains how “text” is defined. vscode gave me nothing.

```julia
"text" => "Name",

```

```julia
 [Dict("name" => states[1][i,1]

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 20, 2022, 10:18pm UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/2 "2022-02-20T22:18:56Z")

</div>

this looks highly contrived, just use CSV.jl.

It looks like `states[1]` is a `N x 3` matrix, so on each row (`i`th row), you have 3 elements, corresponding to what you see in the CSV

* * *

`"text"` is a literal string, it doesn’t need to defined before this because it’s not a variable.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [February 20, 2022, 10:21pm UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/3 "2022-02-20T22:21:52Z")

</div>

Hi there,

I get that bit, it’s just a comprehension and makes sense to me. It’s the relationship of “name” from headers to the “name” in states. Again, I can see what it does but not how it does it.

---

<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:** [February 20, 2022, 10:22pm UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/4 "2022-02-20T22:22:07Z")

</div>

Why Dicts? The code you posted looks pretty nonsensical to me tbh.

What’s wrong with `CSV.read(“mycsv.csv", DataFrame) `?

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [February 20, 2022, 10:26pm UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/5 "2022-02-20T22:26:59Z")

</div>

Hi @nilshg and @jling hope your weekend is going well. I am trying out new things and setting myself tasks. I wanted to see how to put a csv into a Dict to get a better understanding of how Dict works should I need to use it later on.

this is NOT my code I found it on discourse.

there’s nothing wrong with CSV,jl and I do understand how to use it thanks to you good people BUT I wanted to look at this approach to learn.

---

<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:** [February 20, 2022, 10:28pm UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/6 "2022-02-20T22:28:57Z")

</div>

Quite frankly with a bit of background knowledge about your wider goal I don’t think you should bother trying to understand how to wrangle data with `Dict`s.

`Dict`s can of course be a very useful structure at times, but they are nowhere near as important and ubiquitous in Julia as they are in Python.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [February 20, 2022, 10:30pm UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/7 "2022-02-20T22:30:39Z")

</div>

It’s not the array that I am confused about it’s “names” how it relates to headers names.

So how can I research the relationship of “text” =\> “Name” I can figure out what it’s doing but would like to read more. Any ideas where to look please?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 20, 2022, 10:36pm UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/8 "2022-02-20T22:36:43Z")

</div>

[https://docs.julialang.org/en/v1/base/collections/#Core.Pair](https://docs.julialang.org/en/v1/base/collections/#Core.Pair)  
[https://docs.julialang.org/en/v1/base/collections/#Base.Dict](https://docs.julialang.org/en/v1/base/collections/#Base.Dict)

---

<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:** [February 20, 2022, 10:36pm UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/9 "2022-02-20T22:36:47Z")

</div>

Honestly I don’t think there’s much to learn here - the code you’re showing just seems to be (as Jerry says) an insanely contrived way of storing some very simple information in a very complex deeply nested structure (a `Dict` where each value is a vector of `Dict`s for a simple table?!?).

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [February 20, 2022, 10:38pm UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/10 "2022-02-20T22:38:50Z")

</div>

I hear what you are saying but I think there might be a case for Dicts in my approach. I would like to put this query to bed as it’s bugging me 😉

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [February 20, 2022, 10:50pm UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/11 "2022-02-20T22:50:50Z")

</div>

Hi @nilshg and @jling  
I hear you both and respect your opinion. I’ll back off from trying to understand what is going on here. One thought about that, I think I might have taken the example out of context so it might be the BEST solution to a problem I didn’t include in my question.

Here is the place I found it. [the example I was looking at](https://discourse.julialang.org/t/interactive-data-table-in-pluto-jl-via-a-silly-hack/44678/12)

thanks for giving me your counsel

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [February 20, 2022, 11:41pm UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/12 "2022-02-20T23:41:28Z")

</div>

> [@anon69491625](#):
>
> One thought about that, I think I might have taken the example out of context so it might be the BEST solution to a problem I didn’t include in my question.

This is exactly right. That issue is dealing with how to split/store the data (arbitrary data, actually) in a way that is conducive to displaying it as a table using HTML.

I’m not sure which sections you do or don’t understand, but I hope it’s been clear that this _isn’t_ an approach you want to take if you’re actually doing data analysis of any sort.

In any case:

> [@anon69491625](#):
>
> **I don’t understand the relationship of headers “name” to states “name” in this case?**

`states_dict["headers"]` tells you what the keys of each sub-dictionary are, and also gives a “nicer” name to print out as the table header for the column corresponding to that key. So the value under `states_dict["states"][1]["name"]` should print under the header `"Name"` (because that’s the value that goes with `"name"` in the headers dict). If that sounds convoluted, it’s because it is!

As a side note in general, to figure out what code snippets like this do (and how) I suggest working in the REPL. Copy paste that code then play around with `states_dict` result until you have a feel for how it works.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [February 21, 2022, 12:00am UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/13 "2022-02-21T00:00:02Z")

</div>

hi @tomerarnon thank you for taking the time to add your counsel. I am learning a LOT from the good people on discourse. I wanted to make sure that the original thread was cited as I thought I had made an error not linking it in the first place. @nilshg and @jling are being VERY patient with me and I do value their opinion.

What is STILL confusing to me is what the code

```julia
"text" => "Name"

```

actually does. I read the docs that @jling was kind enough to link to ( which I had read before) but I STILL don’t see it. I agree that I should use the REPL more as vscode is not really my cup of tea. I prefer less noise which is what the REPL does. thanks again

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 21, 2022, 12:07am UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/14 "2022-02-21T00:07:43Z")

</div>

it doesn’t do anything, for whatever weird reason,

```julia
"headers" => [Dict("text" => "Name", "value" => "name"), Dict("text" => "Abbreviation", "value" => "abbrev"), Dict("text" => "FIPS", "value" => "fips")]

```

this is a `Pair{String, Vector{Dict{String, String}}}`.

The `"headers"` is a String, and the `[]` is a Vector of Dictionary

don’t do this, don’t use this structure for this data, just don’t.

* * *

```julia

julia> a = 2 => 3;

julia> typeof(a)
Pair{Int64, Int64}

julia> b = (2, 3)
(2, 3)

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

```

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [February 21, 2022, 12:19am UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/15 "2022-02-21T00:19:38Z")

</div>

@jling not sure what you mean by

**don’t do this, don’t use this structure for this data, just don’t.**

It is unclear to me 🙂 just kidding. It made me laugh out loud. Thanks so much for taking the time to revisit this. I have learnt a LOT from this exchange and thanks to all of you for spending time on a Sunday to help out a poor noob.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [February 21, 2022, 2:17am UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/16 "2022-02-21T02:17:19Z")

</div>

[The code that you are referencing](https://discourse.julialang.org/t/interactive-data-table-in-pluto-jl-via-a-silly-hack/44678/25) has a very specific purpose - to wrangle CSV data into a _very specific_ format using Julia `Dict`s, so that it can then be written to a _very specific_ JSON format that is expected by a [Vuetify data table](https://vuetifyjs.com/en/components/data-tables/). Its sole purpose is to allow you to display a nice, interactive data table inside of a Pluto notebook and it’s certainly not something you would typically do…in other words, it’s about the worst example you could have found if you are trying to understand Julia `Dict`s and how one _typically_ uses them…to be fair though, I think if you read from the beginning of that thread, the purpose of that piece of code is quite clear…

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [February 21, 2022, 2:43am UTC](https://discourse.julialang.org/t/setting-up-dict-example-puzzled-by-syntax-and-functionality/76823/17 "2022-02-21T02:43:22Z")

</div>

Hi @mthelm85 this is completely my fault and I took your code out of context and used it incorrectly. I apologize unreservedly. I jumped into your original thread without reading it properly and, obviously, reaped the benefits of my laziness. I’m taking @nilshg, @jling and @tomerarnon collective advise and going back to the drawing board. I thought there were many interesting things in the code that I didn’t understand and thought I would learn from them. Sorry again  
theakson
