# Populating a dictionary using a function with arbitrary keyword arguments

**URL:** https://discourse.julialang.org/t/populating-a-dictionary-using-a-function-with-arbitrary-keyword-arguments/67788
**Category:** New to Julia
**Tags:** question, dictionary
**Created:** [September 7, 2021, 3:25am UTC](https://discourse.julialang.org/t/populating-a-dictionary-using-a-function-with-arbitrary-keyword-arguments/67788 "2021-09-07T03:25:11Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![EricChameroy](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@EricChameroy](https://discourse.julialang.org/u/EricChameroy)
#### Post date: [September 7, 2021, 3:25am UTC](https://discourse.julialang.org/t/populating-a-dictionary-using-a-function-with-arbitrary-keyword-arguments/67788/1 "2021-09-07T03:25:11Z")

</div>

I’m really confused about how to go about populating a dictionary with a function involving arbitrary keyword arguments.

I’ve been practicing Julia by working through a book about Python and re-writing the code snippets using Julia.

This is the Python code snippet that I’m having difficulty with:

```julia
def build_profile(first, last, **user_info):
       """Build a dictionary containing everything we know about a user."""
       user_info['first_name'] = first
       user_info['last_name'] = last
       return user_info

   user_profile = build_profile('albert', 'einstein', location='princeton', field='physics')

```

I came up with something like this, but only the first and last name make their way into the dictionary:

```julia
function build_profile(first, last; user_info...)
    user_info = Dict(:first_name => first, :last_name => last)
    return user_info
end

user_profile = build_profile("albert", "einstein", location="princeton", field="physics")
println(user_profile)

```

What would the equivalent in Julia look like?

---

<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: [September 7, 2021, 5:05am UTC](https://discourse.julialang.org/t/populating-a-dictionary-using-a-function-with-arbitrary-keyword-arguments/67788/2 "2021-09-07T05:05:07Z")

</div>

You’re not using `user_info` inside your function, so I’m not sure why one would expect those keywords to do anything (I see that Python is doing this but I’d argue this is pretty surprising behaviour).

Be that as it may, kwargs in Julia show up in the function as key/val pairs so you can just splat them into your dictionary:

```julia
julia> function build_profile(first, last; user_info...)
           Dict(:first_name => first, :last_name => last, user_info...)
       end
build_profile (generic function with 1 method)

julia> build_profile("albert", "einstein", location="princeton", field="physics")
Dict{Symbol, String} with 4 entries:
  :location => "princeton"
  :field => "physics"
  :last_name => "einstein"
  :first_name => "albert"

```

---

<div class="post-metadata">

### Author: ![EricChameroy](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@EricChameroy](https://discourse.julialang.org/u/EricChameroy)
#### Post date: [September 7, 2021, 6:51am UTC](https://discourse.julialang.org/t/populating-a-dictionary-using-a-function-with-arbitrary-keyword-arguments/67788/3 "2021-09-07T06:51:31Z")

</div>

Thank you for your help! I didn’t think of putting `user_info...` inside of the function. Python doesn’t require it, yet it still works, which is why I was confused as to what was needed for it to work in Julia.

---

<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: [September 7, 2021, 7:56am UTC](https://discourse.julialang.org/t/populating-a-dictionary-using-a-function-with-arbitrary-keyword-arguments/67788/4 "2021-09-07T07:56:37Z")

</div>

Yes, that’s why I checked python to verify that this works. I find it pretty confusing that an unused argument all of a sudden appears in the result in this way, but maybe there’s some deeper python mechanism this is consistent with -someone with better python knowledge than me would have to chime in.

---

<div class="post-metadata">

### Author: ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)
#### Post date: [September 7, 2021, 9:03am UTC](https://discourse.julialang.org/t/populating-a-dictionary-using-a-function-with-arbitrary-keyword-arguments/67788/5 "2021-09-07T09:03:03Z")

</div>

In Python keyword arguments are internally a dictionary, analogous to e.g. class attributes (` __dict__ `).

This is not the case in julia for performance reasons and to avoid allocations.

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [September 7, 2021, 9:14am UTC](https://discourse.julialang.org/t/populating-a-dictionary-using-a-function-with-arbitrary-keyword-arguments/67788/6 "2021-09-07T09:14:33Z")

</div>

Also note that the Julia code wouldn’t work in Python either as written.

---

<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: [September 7, 2021, 9:52am UTC](https://discourse.julialang.org/t/populating-a-dictionary-using-a-function-with-arbitrary-keyword-arguments/67788/7 "2021-09-07T09:52:05Z")

</div>

Sorry I know this is not a Python forum but I’m interested so maybe you’ll indulge me: are you saying that the reason this works in Python is that when I do `def f(x, **my_kwargs)` the function will always have a dictionary object names `my_kwargs` in its scope which I can return? So doing `my_kwargs['new_entry'] = "something"` will write to this existing object?

Actually I hadn’t noticed that there’s never any dictionary explicitly created in the Python function at all, so I guess this must be the case. It’s then just that in Julia the kwargs aren’t a `Dict`:

```julia
julia> f(; kwargs...) = kwargs
f (generic function with 1 method)

julia> f(a = 1, b = "c")
pairs(::NamedTuple) with 2 entries:
  :a => 1
  :b => "c"

```

---

<div class="post-metadata">

### Author: ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)
#### Post date: [September 7, 2021, 10:16am UTC](https://discourse.julialang.org/t/populating-a-dictionary-using-a-function-with-arbitrary-keyword-arguments/67788/8 "2021-09-07T10:16:56Z")

</div>

> [@nilshg](#):
>
> So doing `my_kwargs['new_entry'] = "something"` will write to this existing object?

Yes:

```python
>>> def f(**kwargs):
... kwargs['foo'] = 42
... return kwargs
...
>>> f(bar=9)
{'bar': 9, 'foo': 42}

```
