# Dict from 2d arrays and 1d arrays as keys

**URL:** <https://discourse.julialang.org/t/dict-from-2d-arrays-and-1d-arrays-as-keys/1836>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 2, 2017, 9:38am UTC](https://discourse.julialang.org/t/dict-from-2d-arrays-and-1d-arrays-as-keys/1836 "2017-02-02T09:38:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 2, 2017, 9:38am UTC](https://discourse.julialang.org/t/dict-from-2d-arrays-and-1d-arrays-as-keys/1836/1 "2017-02-02T09:38:19Z")

</div>

I am trying to create a (touple) → value dictionary out of a 2d arrays table of values and a 1d array of keys.

This code (not mine) works…

```julia
using DataArrays, DataFrames

# This works....

orig = ["Epinal", "Bordeaux", "Grenoble"]
prod = ["Fuelwood", "Sawnwood", "Pannels"]
supplytable = wsv"""
prod Epinal Bordeaux Grenoble
Fuelwood 400 700 800
Sawnwood 800 1600 1800
Pannels 200 300 300
"""
supply = Dict( (r[:prod],o) => r[Symbol(o)] for r in eachrow(supplytable), o in orig)

```

This however doesn’t work (“LoadError: ArgumentError: Dict(kv): kv needs to be an iterator of tuples or pairs”):

```julia
# This doesn't work...
s = ["Chêne pédonculé", "Chêne sessile", "Hêtre", "Pin sylvestre"]
σtable = [[0.037502535947712	0.016082745098039	0.027797176470588	-0.025589882352942]
          [0.016082745098039	0.015177019607843	0.018791960784314	-0.102880470588234]
          [0.027797176470588	0.018791960784314	0.031732078431373	-0.166391058823529]
          [-0.025589882352942	-0.102880470588234	-0.166391058823529	2.02950454411765]]
σ = Dict( (i,j) => σtable[i,j] for i in s, j in s)

```

What’s the difference ??  
I’m sure I can find alternatives, but I am curious why it doesn’t work, as I tried to mimic the above code that works…

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 2, 2017, 9:51am UTC](https://discourse.julialang.org/t/dict-from-2d-arrays-and-1d-arrays-as-keys/1836/2 "2017-02-02T09:51:05Z")

</div>

You are trying to index `σtable` with `i` and `j` with strings, which doesn’t make sense since it’s an array. Try with `σ = Dict( (s[i], s[j]) => σtable[i,j] for i in 1:length(s), j in 1:length(s) )`.

By the way, for this kind of data structure, you might be interested in the NamedArrays package instead of using a dict.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 2, 2017, 10:03am UTC](https://discourse.julialang.org/t/dict-from-2d-arrays-and-1d-arrays-as-keys/1836/3 "2017-02-02T10:03:08Z")

</div>

sure, thank you…

Also working:  
`σ = Dict((i,j) => σtable[i_ix,j_ix] for (i_ix,i) in enumerate(s), (j_ix,j) in enumerate(s))`
