# 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:** 1\
**Showing post:** 3

<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?

---

_[View the full topic](https://discourse.julialang.org/t/initiate-multiple-arrays-from-a-set-of-names-with-comprehension/91402)._
