# Alternatives to Dictionaries in Julia

**URL:** https://discourse.julialang.org/t/alternatives-to-dictionaries-in-julia/94901
**Category:** New to Julia
**Tags:** dictionaries
**Created:** [February 20, 2023, 6:55am UTC](https://discourse.julialang.org/t/alternatives-to-dictionaries-in-julia/94901 "2023-02-20T06:55:48Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![ivoytov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivoytov/32/42480_2.png) [@ivoytov](https://discourse.julialang.org/u/ivoytov)
#### Post date: [February 20, 2023, 6:55am UTC](https://discourse.julialang.org/t/alternatives-to-dictionaries-in-julia/94901/1 "2023-02-20T06:55:48Z")

</div>

I’ve been slowly porting my Java and Python code to Julia and frequently come across statements like “dictionaries are less commonly used in Julia” [1](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/2)

I catch myself using dictionaries all the time in Julia, as direct replacement for (many) maps in Java and Python dictionaries. You get the advantage of a set for the key, easy way to filter down on values while being able to link each value to its key.

Am I doing this wrong? Should I be replacing maps and dicts with some other data structure?

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [February 20, 2023, 6:57am UTC](https://discourse.julialang.org/t/alternatives-to-dictionaries-in-julia/94901/2 "2023-02-20T06:57:36Z")

</div>

DataFrames and NamedTuples are popular alternatives, depending on the use case. Dicts are also popular, as are Dictionaries.jl dicts.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [February 20, 2023, 7:19am UTC](https://discourse.julialang.org/t/alternatives-to-dictionaries-in-julia/94901/3 "2023-02-20T07:19:03Z")

</div>

What kind of `Dict` are you using? I would begin to worry if one or both of the type parameters to `Dict` is frequently `Any` or an abstract type.

Also see

> **[JuliaCollections](https://github.com/JuliaCollections)**
>
> Collections, Data Structures, and Algorithms for Julia - JuliaCollections

---

<div class="post-metadata">

### Author: ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)
#### Post date: [February 20, 2023, 7:57am UTC](https://discourse.julialang.org/t/alternatives-to-dictionaries-in-julia/94901/4 "2023-02-20T07:57:41Z")

</div>

It’s not wrong to use `Dict`s. They are a basic data structure used in all kinds of languages. It’s perfectly idiomatic to use `Dict`s in Julia where they make sense to use algorithmically  
That being said, Python does use dicts way more that we would in Julia.

- In Python, all instances of custom classes contain a `dict` by default, not so in Julia
- In Python, keyword arguments are stored in a `dict`, whereas in Julia, they are in a `NamedTuple`
- In Julia, `NamedTuple`s can often be used instead of `Dict`s with benefit, if the set of keys is both small and known at compile time.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [February 20, 2023, 8:03am UTC](https://discourse.julialang.org/t/alternatives-to-dictionaries-in-julia/94901/5 "2023-02-20T08:03:22Z")

</div>

There’s nothing wrong with dictionaries per se, but depending on how you use them other alternatives may be relatively more attractive in Julia.

- Named tuples are conveniently available. They have some tradeoffs though in being immutable and heterogeneously typed. Particularly useful when you want to pass around small amounts (number of fields) of read-only data.

- Custom types (`struct` and `mutable struct`) are low overhead to create and more practical than dictionaries if you want to take advantage of dispatch.

- Non-concretely typed dictionaries can cause inference failures, which can be a significant concern in Julia.

---

<div class="post-metadata">

### Author: ![ivoytov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivoytov/32/42480_2.png) [@ivoytov](https://discourse.julialang.org/u/ivoytov)
#### Post date: [February 20, 2023, 12:00pm UTC](https://discourse.julialang.org/t/alternatives-to-dictionaries-in-julia/94901/6 "2023-02-20T12:00:20Z")

</div>

My typical use case is to take a list of limited size like the list of stocks owned in a portfolio `Vector{Security}`, then for each one get the share price and shares outstanding. Now you have `Dict{Security, Tuple(Float, BigInt}}`. Then you can filter, do additional processing, and ultimately return a dictionary of stock and transactions `Dict{Security, Order}`.

Does this make sense?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 20, 2023, 12:17pm UTC](https://discourse.julialang.org/t/alternatives-to-dictionaries-in-julia/94901/7 "2023-02-20T12:17:00Z")

</div>

> [@ivoytov](#):
>
> then for each one get the share price and shares outstanding. Now you have `Dict{Security, Tuple(Float, BigInt}}`

Offtopic: you need a `BigInt` for a count of shares? It seems like `Int64` should always suffice.

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [February 20, 2023, 12:17pm UTC](https://discourse.julialang.org/t/alternatives-to-dictionaries-in-julia/94901/8 "2023-02-20T12:17:51Z")

</div>

It sounds like tabular data to me. As others have mentioned, a `DataFrame` might be suitable for that.

You would have a `DataFrame` of all stocks in all portfolios. You can then group, filter, aggregate, join, … This should sound familiar if you have experience with relational databases and SQL.

But anyway… there is nothing wrong with dictionaries!

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [February 20, 2023, 12:41pm UTC](https://discourse.julialang.org/t/alternatives-to-dictionaries-in-julia/94901/9 "2023-02-20T12:41:00Z")

</div>

If you need access by key, and this is the most common access pattern, then dictionaries is the natural structure to use!  
Otherwise, you may want to look at simple arrays, like `data = [(security=Security(...), price=..., shares=...), (security=Security(...), price=..., shares=...), ...]`. It’s basically a lightweight table, and you can use all kinds of common tabular operations, as well as functions from the vast Julia array ecosystem.  
Converting such a struct to a dict for fast lookup is also easy: `prices = Dict(r.security => r.price for r in data)`.

---

<div class="post-metadata">

### Author: ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)
#### Post date: [February 20, 2023, 2:11pm UTC](https://discourse.julialang.org/t/alternatives-to-dictionaries-in-julia/94901/10 "2023-02-20T14:11:40Z")

</div>

~~Maybe this was already said, but I didn’t see it skimming the other comments,~~ so I’d just add that it’s pretty easy to end up with a `Dict{Any, Any}` which really messes up type inference (and therefore performance). If it’s possible to annotate the types at the instantiation of the Dict like `d =Dict{Symbol, Int}()`, it will help ward off some of the problems that the “don’t use `Dict`” advice is trying to prevent.

Edit: oops sorry @GunnarFarneback

> [@GunnarFarneback](#):
>
> Non-concretely typed dictionaries can cause inference failures, which can be a significant concern in Julia.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [February 20, 2023, 3:59pm UTC](https://discourse.julialang.org/t/alternatives-to-dictionaries-in-julia/94901/11 "2023-02-20T15:59:53Z")

</div>

> [@stevengj](#):
>
> Offtopic: you need a `BigInt` for a count of shares?

We can all dream 😄
