# How to collect keyword arguments in a Dict?

**URL:** <https://discourse.julialang.org/t/how-to-collect-keyword-arguments-in-a-dict/6693>\
**Category:** General Usage\
**Created:** [October 26, 2017, 7:45am UTC](https://discourse.julialang.org/t/how-to-collect-keyword-arguments-in-a-dict/6693 "2017-10-26T07:45:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksmet1977](https://avatars.discourse-cdn.com/v4/letter/k/b5ac83/32.png) [@ksmet1977](https://discourse.julialang.org/u/ksmet1977)\
**Post date:** [October 26, 2017, 7:45am UTC](https://discourse.julialang.org/t/how-to-collect-keyword-arguments-in-a-dict/6693/1 "2017-10-26T07:45:17Z")

</div>

How can I get the keyword arguments and their default values of a function in a dict in Julia?

E.g:

```
function foo(x; a = 1, b = 2, c= 3)
  # How do I get a dict of keyword arguments: Dict(a=>1, b=>2,c=3) ??,
  # so I can pass this Dict easily to another generic function taking v 
  # variable keyword arguments for further processing
end

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 26, 2017, 8:40am UTC](https://discourse.julialang.org/t/how-to-collect-keyword-arguments-in-a-dict/6693/2 "2017-10-26T08:40:07Z")

</div>

```julia
function foo(; args...)
    Dict(args)
end

```

---

<div class="post-metadata">

**Author:** ![ksmet1977](https://avatars.discourse-cdn.com/v4/letter/k/b5ac83/32.png) [@ksmet1977](https://discourse.julialang.org/u/ksmet1977)\
**Post date:** [October 26, 2017, 9:01am UTC](https://discourse.julialang.org/t/how-to-collect-keyword-arguments-in-a-dict/6693/3 "2017-10-26T09:01:28Z")

</div>

I know how to do that. But I want to be able to do it for a function where the keyword arguments are explicitely defined in the function/method definition, like in the example.  
Any thoughts? Thanks.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [October 26, 2017, 9:05am UTC](https://discourse.julialang.org/t/how-to-collect-keyword-arguments-in-a-dict/6693/4 "2017-10-26T09:05:29Z")

</div>

Note that cross-posting with StackOverflow is discouraged - if you do it it’s nice to provide the link: [collect function keyword arguments in dict in julia - Stack Overflow](https://stackoverflow.com/questions/46948486/collect-function-keyword-arguments-in-dict-in-julia)  
Also, “I know how to do that” easily comes across as impolite.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [October 26, 2017, 9:10am UTC](https://discourse.julialang.org/t/how-to-collect-keyword-arguments-in-a-dict/6693/5 "2017-10-26T09:10:10Z")

</div>

Anyway, if you want to do that, the easiest way is to define the Dict first, like in the answer given on SO:

```julia
args = Dict(:a => 3, :b => 4)
foo(;args...)

```

---

<div class="post-metadata">

**Author:** ![ksmet1977](https://avatars.discourse-cdn.com/v4/letter/k/b5ac83/32.png) [@ksmet1977](https://discourse.julialang.org/u/ksmet1977)\
**Post date:** [October 26, 2017, 9:26am UTC](https://discourse.julialang.org/t/how-to-collect-keyword-arguments-in-a-dict/6693/6 "2017-10-26T09:26:43Z")

</div>

Thanks. I didn’t want to come across a impolite.  
Anyway, I had thought of that, but it wouldn’t work for what I want to do (I think), which is to define a function with default values for the keyword arguments, but that function also has one keyword argument that either contains ‘nothing’ or a Dict with a subset of the keyword arguments of that function. The value of any keyword arguments that are keys in the Dict will be replaced by the corresponding values in the Dict. That way is easy to write a general function that has a lot of keyword arguments with specific defaults, but where subsets of those can be easily changes by passing a Dict (for example, which comes in handy when testing different models with varying parameters sets).  
Any thoughts on how to go about this in perhaps another way? (I used this method in Python by getting the passed arguments by creating a copy of the locals() just after entering the function, so I was hoping to do something similar in Julia). Many thanks for any advice.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 26, 2017, 9:40am UTC](https://discourse.julialang.org/t/how-to-collect-keyword-arguments-in-a-dict/6693/7 "2017-10-26T09:40:20Z")

</div>

> [@ksmet1977](#):
>
> a general function that has a lot of keyword arguments with specific defaults, but where subsets of those can be easily changes by passing a Dict (for example, which comes in handy when testing different models with varying parameters sets)

If you have lots of keywords arguments, consider defining a `struct` to contain them. To change a subset of them, see the nice keyword constructor syntax of

> **[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

or the packages mentioned there.

---

<div class="post-metadata">

**Author:** ![ksmet1977](https://avatars.discourse-cdn.com/v4/letter/k/b5ac83/32.png) [@ksmet1977](https://discourse.julialang.org/u/ksmet1977)\
**Post date:** [October 26, 2017, 11:23am UTC](https://discourse.julialang.org/t/how-to-collect-keyword-arguments-in-a-dict/6693/8 "2017-10-26T11:23:05Z")

</div>

Thanks.

---

<div class="post-metadata">

**Author:** ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)\
**Post date:** [April 13, 2020, 7:07am UTC](https://discourse.julialang.org/t/how-to-collect-keyword-arguments-in-a-dict/6693/9 "2020-04-13T07:07:51Z")

</div>

Any news on this? It is possible with the new version of Julia?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 13, 2020, 8:04am UTC](https://discourse.julialang.org/t/how-to-collect-keyword-arguments-in-a-dict/6693/10 "2020-04-13T08:04:55Z")

</div>

```julia
f(; args...) = Dict(args)

```
