# Initializing a Dict, is there a more Julia way to do it?

**URL:** <https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681>\
**Category:** New to Julia\
**Created:** [August 2, 2021, 2:12pm UTC](https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681 "2021-08-02T14:12:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![saxasmu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saxasmu/32/26668_2.png) [@saxasmu](https://discourse.julialang.org/u/saxasmu)\
**Post date:** [August 2, 2021, 2:12pm UTC](https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681/1 "2021-08-02T14:12:49Z")

</div>

I want to create a dict with as keys Integers in the range 1:lines and as value an empty Vector of Int.  
Coming from Python I found this the shortest way to initialize this Dict:

`d::Dict{Int, Vector{Int}} = Dict([(k, []) for k in 1:lines])`

It works but I was wondering is there a better or more Julia way to do this?

---

<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:** [August 2, 2021, 2:15pm UTC](https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681/2 "2021-08-02T14:15:48Z")

</div>

You can use a dict comprehension like (I believe?) in Python:

```julia
julia> Dict(k => Int[] for k in 1:5)
Dict{Int64, Vector{Int64}} with 5 entries:
  5 => []
  4 => []
  2 => []
  3 => []
  1 => []

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 2, 2021, 2:18pm UTC](https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681/3 "2021-08-02T14:18:17Z")

</div>

Note, the type annotation seems to be ignored when I run your code. Not sure why that is, but the RHS of your code creates a `Dict{Int, Vector{Any}}`, and it doesn’t automatically convert when you add that type annotation.

---

<div class="post-metadata">

**Author:** ![saxasmu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saxasmu/32/26668_2.png) [@saxasmu](https://discourse.julialang.org/u/saxasmu)\
**Post date:** [August 2, 2021, 2:22pm UTC](https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681/4 "2021-08-02T14:22:20Z")

</div>

Thanks works very well!

---

<div class="post-metadata">

**Author:** ![saxasmu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saxasmu/32/26668_2.png) [@saxasmu](https://discourse.julialang.org/u/saxasmu)\
**Post date:** [August 2, 2021, 2:24pm UTC](https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681/5 "2021-08-02T14:24:04Z")

</div>

When I run it in my script both nilshg his solution and my code generate the right types!  
I’m running with v1.6.2

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 2, 2021, 2:28pm UTC](https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681/6 "2021-08-02T14:28:56Z")

</div>

Weird!

```julia
julia> function foo()
       lines = 5
       d::Dict{Int, Vector{Int}} = Dict([(k, []) for k in 1:lines])
       end;

julia> versioninfo()
Julia Version 1.6.1
Commit 6aaedecc44 (2021-04-23 05:59 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)

julia> foo()
Dict{Int64, Vector{Any}} with 5 entries:
  5 => []
  4 => []
  2 => []
  3 => []
  1 => []

```

I’ll check the history to see if some change would have affected this.

---

<div class="post-metadata">

**Author:** ![saxasmu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saxasmu/32/26668_2.png) [@saxasmu](https://discourse.julialang.org/u/saxasmu)\
**Post date:** [August 2, 2021, 2:34pm UTC](https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681/7 "2021-08-02T14:34:42Z")

</div>

When I run your code in the REPL I get same result!  
but in my script running:

```julia
    d::Dict{Int, Vector{Int}} = Dict(k => Int[] for k in 1:lines)
    @show typeof(d)

```

I get:  
`typeof(d) = Dict{Int64, Vector{Int64}}`

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 2, 2021, 2:39pm UTC](https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681/8 "2021-08-02T14:39:10Z")

</div>

Yes, your code has `Int[]` and mine does not.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 2, 2021, 2:45pm UTC](https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681/9 "2021-08-02T14:45:49Z")

</div>

This is expected because assignments in Julia always just return the right-hand side. That means the return value of the expression `d::Dict{Int, Vector{Int}} = Dict([(k, []) for k in 1:lines])` is unaffected by the type annotation on the left-hand side.

```julia
julia> function foo()
       lines = 5
       d::Dict{Int, Vector{Int}} = Dict([(k, []) for k in 1:lines])
       d
       end;

```

should do what you want.

---

<div class="post-metadata">

**Author:** ![saxasmu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saxasmu/32/26668_2.png) [@saxasmu](https://discourse.julialang.org/u/saxasmu)\
**Post date:** [August 2, 2021, 2:48pm UTC](https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681/10 "2021-08-02T14:48:19Z")

</div>

Yes that makes it very clear, thanks very much to all!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 2, 2021, 2:52pm UTC](https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681/11 "2021-08-02T14:52:19Z")

</div>

Interesting!

The `Dict{Int, Vector{Any}}` is still materialized at some point before the conversion, right? So this code is still inefficient, right?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 2, 2021, 2:54pm UTC](https://discourse.julialang.org/t/initializing-a-dict-is-there-a-more-julia-way-to-do-it/65681/12 "2021-08-02T14:54:56Z")

</div>

Yes
