# Error creating a Dict with symbols as indices

**URL:** https://discourse.julialang.org/t/error-creating-a-dict-with-symbols-as-indices/109367
**Category:** New to Julia
**Created:** [January 28, 2024, 8:13pm UTC](https://discourse.julialang.org/t/error-creating-a-dict-with-symbols-as-indices/109367 "2024-01-28T20:13:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![orebas](https://avatars.discourse-cdn.com/v4/letter/o/bbe5ce/32.png) [@orebas](https://discourse.julialang.org/u/orebas)
#### Post date: [January 28, 2024, 8:13pm UTC](https://discourse.julialang.org/t/error-creating-a-dict-with-symbols-as-indices/109367/1 "2024-01-28T20:13:22Z")

</div>

I am trying to emulate some code I have been given, which returns a Dict that has model states from a ModelingToolkit model as indices. But the below code gives an error. Here’s the MWE:

```julia
using ModelingToolkit

function main()
	@parameters a
	@variables t x1(t)
	D = Differential(t)
	states = [x1]
	parameters = [a]

	@named model = ODESystem([
			D(x1) ~ a * x1,
		], t, states, parameters)

	state_vec = ModelingToolkit.states(model)
	state_dict = Dict[]
	state_dict[state_vec[1]] = "test"
end
main()

```

and here is the error:

```julia
ERROR: LoadError: ArgumentError: invalid index: x1(t) of type SymbolicUtils.BasicSymbolic{Real}
Stacktrace:
  [1] to_index(i::SymbolicUtils.BasicSymbolic{Real})
    @ Base ./indices.jl:300
  [2] to_index(A::Vector{Dict}, i::SymbolicUtils.BasicSymbolic{Real})
    @ Base ./indices.jl:277
  [3] _to_indices1(A::Vector{Dict}, inds::Tuple{Base.OneTo{Int64}}, I1::SymbolicUtils.BasicSymbolic{Real})
    @ Base ./indices.jl:359
  [4] to_indices
    @ Base ./indices.jl:354 [inlined]
  [5] to_indices
    @ Base ./indices.jl:345 [inlined]
  [6] setindex!(A::Vector{Dict}, v::String, I::SymbolicUtils.BasicSymbolic{Real})
    @ Base ./abstractarray.jl:1393
  [7] main()
    @ Main ~/learning/ODETests/error.jl:16
  [8] top-level scope
    @ ~/learning/ODETests/error.jl:18
  [9] include(fname::String)
    @ Base.MainInclude ./client.jl:489
 [10] top-level scope
    @ REPL[1]:1
in expression starting at /home/orebas/learning/ODETests/error.jl:18

```

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [January 28, 2024, 8:27pm UTC](https://discourse.julialang.org/t/error-creating-a-dict-with-symbols-as-indices/109367/2 "2024-01-28T20:27:30Z")

</div>

```julia
state_dict = Dict[]

```

This is a `Vector{Dict}`.  
What you want is:

```julia
julia> state_dict = Dict()
Dict{Any, Any}()

```

Or you give the types, e.g.:

```julia
julia> state_dict = Dict{SymbolicUtils.BasicSymbolic{Real},String}()

```

Can’t test this right now, but perhaps you get what I mean and find the proper type.

---

<div class="post-metadata">

### Author: ![orebas](https://avatars.discourse-cdn.com/v4/letter/o/bbe5ce/32.png) [@orebas](https://discourse.julialang.org/u/orebas)
#### Post date: [January 28, 2024, 8:36pm UTC](https://discourse.julialang.org/t/error-creating-a-dict-with-symbols-as-indices/109367/3 "2024-01-28T20:36:23Z")

</div>

Thank you, this solved my problem.

---

<div class="post-metadata">

### Author: ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)
#### Post date: [January 28, 2024, 8:45pm UTC](https://discourse.julialang.org/t/error-creating-a-dict-with-symbols-as-indices/109367/4 "2024-01-28T20:45:14Z")

</div>

> [@oheil](#):
>
> ```julia-auto
> julia> state_dict = Dict{SymbolicUtils.BasicSymbolic{Real},String}()
> 
> ```

A slightly shorter version:

```julia-auto
    state_dict = Dict{typeof(state_vec[]),String}()

```

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [January 29, 2024, 10:02am UTC](https://discourse.julialang.org/t/error-creating-a-dict-with-symbols-as-indices/109367/5 "2024-01-29T10:02:59Z")

</div>

Or even shorter and clearer 🙂

```julia
Dict{eltype(state_vec),String}()

```
