# Initiate multiple arrays from a set of names with comprehension

**URL:** <https://discourse.julialang.org/t/initiate-multiple-arrays-from-a-set-of-names-with-comprehension/91402>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 8, 2022, 3:10am UTC](https://discourse.julialang.org/t/initiate-multiple-arrays-from-a-set-of-names-with-comprehension/91402 "2022-12-08T03:10:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [December 8, 2022, 3:10am UTC](https://discourse.julialang.org/t/initiate-multiple-arrays-from-a-set-of-names-with-comprehension/91402/1 "2022-12-08T03:10:17Z")

</div>

I’m trying to understand how to initiate multiple arrays from a set of names. To initiate a new vector I would do something like

```julia
j = Vector{Float64}(undef,10)

```

```julia
typeof(j)
Vector{Float64} (alias for Array{Float64, 1})

```

but suppose I want to initiate multiple vectors with a set of names.

```julia
entries = ("a", "b","c")

```

something like

```julia
[x = Vector{Float64}(undef,10) for x in entries]

```

creates the vectors but does not assign them to the variables in entries.

```julia
3-element Vector{Vector{Float64}}:
 [0.0, 6.52171459e-315, 5.43316358e-314, 0.0, 1.5e-323, 5.43316358e-314, 2.0e-323, 1.0e-323, 5.43316358e-314, 3.5e-323]
 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6.52171459e-315]
 [1.0e-323, 5.43316358e-314, 5.0e-323, 3.0e-323, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

```

but then

```julia
typeof(b)
ERROR: UndefVarError: b not defined

```

likewise

```julia
[x => Vector{Float64}(undef,10) for x in entries]

3-element Vector{Pair{String, Vector{Float64}}}:
 "a" => [5.4e-323, 3.0e-323, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 "b" => [2.0e-323, 5.433169509e-314, 2.5e-323, 1.0e-323, 5.433169509e-314, 4.0e-323, 1.0e-323, 5.433169509e-314, 5.4e-323, 3.0e-323]
 "c" => [0.0, 0.0, 0.0, 0.0, 0.0, 6.52171459e-315, 5.433169509e-314, 0.0, 2.0e-323, 5.433169509e-314]

```

Returns the same `UndefVarError` when the variables are called. I know I am missing something elementary here because the behavior is consistent when I try mapping.

```julia
map(x-> Vector{Float64}(undef,10),ent)

```

Thanks for your patience! Any pointers would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [December 8, 2022, 5:08am UTC](https://discourse.julialang.org/t/initiate-multiple-arrays-from-a-set-of-names-with-comprehension/91402/3 "2022-12-08T05:08:09Z")

</div>

awesome thanks so much this is really helpful! I’m not sure if its because I’m using version 1.8.3 but when I enter in

```julia
dict = [x => Vector{Float64}(undef, 10) for x in entries]

```

I don’t get a dictionary but a vector

```julia
julia> dict = [x => Vector{Float64}(undef, 10) for x in entries]
3-element Vector{Pair{String, Vector{Float64}}}:
 "a" => [5.3696034695e-314, 0.0, 1.0e-323, 5.3696034695e-314, 1.5e-323, 1.0e-323, 5.3696034695e-314, 3.0e-323, 1.0e-323, 5.3696034695e-314]
 "b" => [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6.52171459e-315, 5.3696034695e-314, 0.0]
 "c" => [4.4e-323, 3.0e-323, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

julia> typeof(dict)
Vector{Pair{String, Vector{Float64}}} (alias for Array{Pair{String, Array{Float64, 1}}, 1})

```

Then when I try to access the entries I get an error

```julia
julia> dict["a"]
ERROR: ArgumentError: invalid index: "a" of type String
Stacktrace:
 [1] to_index(i::String)
   @ Base ./indices.jl:300
 [2] to_index(A::Vector{Pair{String, Vector{Float64}}}, i::String)
   @ Base ./indices.jl:277
 [3] to_indices
   @ ./indices.jl:333 [inlined]
 [4] to_indices
   @ ./indices.jl:325 [inlined]
 [5] getindex(A::Vector{Pair{String, Vector{Float64}}}, I::String)
   @ Base ./abstractarray.jl:1241
 [6] top-level scope
   @ REPL[161]:1

```

But if I try

```julia
dict2 =Dict([x => Vector{Float64}(undef, 10) for x in entries])

```

it seems to work. Also is there a way to do this without using a dictionary? My ultimate goal is to initiate these vectors in a function so that I can populate them and then add them to a DataFrame like this via bkamins post here [How to initialize empty dataframe of specified size](https://discourse.julialang.org/t/how-to-initialize-empty-dataframe-of-specified-size/67421)

```julia
julia> function test2()
           nt = (a=Vector{Int}(undef, 10^6),
                 b=Vector{String}(undef, 10^6),
                 c=Vector{Int}(undef, 10^6))
           for i in 1:10^6
               nt.a[i] = 1
               nt.b[i] = "1"
               nt.c[i] = 1.0
           end
           return DataFrame(nt, copycols=false)
       end
test2 (generic function with 1 method)

```

Will using a dictionary as opposed to just vectors affect performance?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 8, 2022, 6:05am UTC](https://discourse.julialang.org/t/initiate-multiple-arrays-from-a-set-of-names-with-comprehension/91402/4 "2022-12-08T06:05:31Z")

</div>

> [@devvinish](#):
>
> ```julia
> julia> entries = ("a", "b","c")
> ("a", "b", "c")
> 
> julia> dict = [x => Vector{Float64}(undef, 10) for x in entries]
> Dict{Any,Any} with 3 entries:
> "a" => [undef, undef, undef, undef, undef, undef, undef, undef, undef, undef]
> "c" => [undef, undef, undef, undef, undef, undef, undef, undef, undef, undef]
> "b" => [undef, undef, undef, undef, undef, undef, undef, undef, undef, undef]
> 
> ```

This is not valid syntax for creating a dictionary, and I don’t think it ever has been. Has this code been run on an actual Julia instance, or has it been ‘generated’ somehow?

> [@phantom](#):
>
> `dict2 =Dict([x => Vector{Float64}(undef, 10) for x in entries])`

Remove the `[]` for better performance.

In general, you should never dynamically create variables from strings. Using a dictionary is a much better way, though if you ultimately need this for a Dataframe, there may be other approaches. But don’t dynamically create variables.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 8, 2022, 1:13pm UTC](https://discourse.julialang.org/t/initiate-multiple-arrays-from-a-set-of-names-with-comprehension/91402/5 "2022-12-08T13:13:00Z")

</div>

Seems very likely that @devvinish’s post is chatGPT nonsense.

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [December 8, 2022, 4:50pm UTC](https://discourse.julialang.org/t/initiate-multiple-arrays-from-a-set-of-names-with-comprehension/91402/6 "2022-12-08T16:50:31Z")

</div>

If you know the names of the variables _a priori_, you can do

```julia
(a, b, c) = [Vector{Float64}(undef, 10) for _ in 1:3]

```

But if you just have `entries` (and if the elements of `entries` can be arbitrary), then creating a dictionary is probably the way to go.

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [December 8, 2022, 8:54pm UTC](https://discourse.julialang.org/t/initiate-multiple-arrays-from-a-set-of-names-with-comprehension/91402/7 "2022-12-08T20:54:59Z")

</div>

Thanks so much for help clearing that up, this is super helpful. If you don’t mind a quick follow up why does

```julia
dict2 =Dict(x => Vector{Float64}(undef, 10) for x in entries)

julia> @benchmark dict3 =Dict(x => Vector{Float64}(undef, 10) for x in entries)
BenchmarkTools.Trial: 10000 samples with 199 evaluations.
 Range (min … max): 440.744 ns … 1.789 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 495.603 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 530.629 ns ± 83.013 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

     ▂▅███▇▇▆▅▄▄▄▃▃▃▃▃▃▃▃▂▂▂▁▁▁▁▁▁▁ ▁▁▁▁▁▁▁▁▁▁ ▁▁ ▃
  ▄▂▆███████████████████████████████▇████▇████████████████▇▇▆▇ █
  441 ns Histogram: log(frequency) by time 813 ns <

 Memory estimate: 976 bytes, allocs estimate: 8.

```

work faster than

```julia
dict2 =Dict([x => Vector{Float64}(undef, 10) for x in entries])

julia> @benchmark dict3 =Dict([x => Vector{Float64}(undef, 10) for x in entries])
BenchmarkTools.Trial: 10000 samples with 194 evaluations.
 Range (min … max): 511.387 ns … 1.157 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 560.778 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 599.912 ns ± 89.956 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

    ▁▅▇█▇▇▆▅▅▄▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▁▁▁▁ ▁ ▁ ▁▁ ▁▁▁▁▁▁▁▁▁ ▁ ▂
  ▅██████████████████████████████████▇████████████████▇██▇▇▇▇▆ █
  511 ns Histogram: log(frequency) by time 910 ns <

 Memory estimate: 1.06 KiB, allocs estimate: 9.

```

Is it because the `Dict()` function doesn’t have to go through an additional layer of comprehension?

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [December 8, 2022, 8:56pm UTC](https://discourse.julialang.org/t/initiate-multiple-arrays-from-a-set-of-names-with-comprehension/91402/8 "2022-12-08T20:56:01Z")

</div>

awesome thanks! This works great!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 8, 2022, 11:21pm UTC](https://discourse.julialang.org/t/initiate-multiple-arrays-from-a-set-of-names-with-comprehension/91402/9 "2022-12-08T23:21:29Z")

</div>

It’s because `Dict([])` first creates a vector which is then converted to a dictionary. `Dict()` directly creates the dictionary.
