# Elegantly create Dicts of parameters

**URL:** https://discourse.julialang.org/t/elegantly-create-dicts-of-parameters/20471
**Category:** General Usage
**Tags:** question
**Created:** [February 5, 2019, 1:48pm UTC](https://discourse.julialang.org/t/elegantly-create-dicts-of-parameters/20471 "2019-02-05T13:48:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)
#### Post date: [February 5, 2019, 1:48pm UTC](https://discourse.julialang.org/t/elegantly-create-dicts-of-parameters/20471/1 "2019-02-05T13:48:52Z")

</div>

Hey there,

I’m trying to write a script that creates dictionaries with simulation parameters.  
I need these to eventually call  
`pmap(job_function, parameter_dicts)`  
where each worker gets its own parameter set and does its thing.

My question is how to elegantly create these dictionaries.  
So far the only way I’ve been able to come up with is this:

```julia
# assign all parameters (optionally Vector of values)
param1 = [1,2]
param2 = 5
param3 = [4,5]
....
param20 = [10,12,15]

parameter_dicts = Dict[]

# Iterate over all possible parameter combinations
for param1 ∈ param1, 
    param2 ∈ param2,
    param3 ∈ param3, 
    ... , 
    param20 ∈ param20
   #Create dict with the parameters
   d = Dict( :param1 => param1,
                  :param2 => param2,
                  :param3 => param3,
                  ...
                  :param20 => param20)
  push!(parameter_dicts, d)
end

```

which is obviously a very long and unreadable script.  
Suggestions would be welcome!

---

<div class="post-metadata">

### Author: ![under-Peter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/under-peter/32/3626_2.png) [@under-Peter](https://discourse.julialang.org/u/under-Peter)
#### Post date: [February 5, 2019, 2:30pm UTC](https://discourse.julialang.org/t/elegantly-create-dicts-of-parameters/20471/2 "2019-02-05T14:30:28Z")

</div>

Hi,  
to do the same thing you’re doing with the loop somewhat more concise, you can do

```julia
# assign all parameters (optionally Vector of values)
param1 = [1,2]
param2 = 5
param3 = [4,5]
....
param20 = [10,12,15]
params = [parm1, param2, param3, ..., param20]
paramnames = (:param1, :param2, :param3, ... , :param20)
 
parameterdicts = [Dict((paramnames .=> params)) for params in Iterators.product(params...)]

```

Here, `Iterators.product` gives you all possible combinations of the arguments you provide.  
I also use `broadcasting` to combine parameter-names and their values into pairs.  
This is the same approach you use but (possibly) a bit more concise.

To organise parameters I’d take a look at the package [https://github.com/mauro3/Parameters.jl](https://github.com/mauro3/Parameters.jl) which provides structs with default values to save parameters.

I’m also not sure about the use of dictionaries in this case, since depending on your params (e.g. having some `Int` and Strings), the dictionary will have `Any` value type, that is, if you access it, the compiler doesn’t know what type it returns. That _might_ be an issue.

Instead you could use `NamedTuple`s, which carry type information about all their fields.  
To use that instead, you’d have to change the dictionary part above to:

```julia
NamedTuple{paramnames}(params) 

```

Maybe worth a quick test-run.

---

<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: [February 5, 2019, 2:54pm UTC](https://discourse.julialang.org/t/elegantly-create-dicts-of-parameters/20471/3 "2019-02-05T14:54:04Z")

</div>

Have you considered:

> **[GitHub - mauro3/Parameters.jl: Types with default field values, keyword...](https://github.com/mauro3/Parameters.jl)**
>
> Types with default field values, keyword constructors and (un-)pack macros - GitHub - mauro3/Parameters.jl: Types with default field values, keyword constructors and (un-)pack macros

---

<div class="post-metadata">

### Author: ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)
#### Post date: [February 5, 2019, 2:59pm UTC](https://discourse.julialang.org/t/elegantly-create-dicts-of-parameters/20471/4 "2019-02-05T14:59:46Z")

</div>

Thank you for the inspiration!  
`Iterators.product` did the trick.

Type stability is really not a problem in this case because all computations happen  
behind additional function barriers.

This seems like a good solution:

```julia
pset = Dict(
   param1 = [1,2],
   param2 = 5,
   param3 = [4,5])

map(vals -> Dict(keys(pset) .=> vals), Iterators.product(values(pset)...))[:]
```
