Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Dictionaries Managing Keys

Must a dict parameter in a function necessarily be preceded by **?

I saw on another tutorial on YouTube that when passing a dict to a function, the corresponding parameter should be preceded by **, e.g. my_function(**dict_param, reg_param). But when I tried to use this syntax in the code challenge I got an invalid syntax error. Can you advise? Thanks

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

** unpacks a dictionary and turns its keys and values into keywords and values. For example, the following two lines are exactly the same.

call_me(**{'name': 'Kenneth', 'number': '555-555-5555'})
call_me(name='Kenneth', number='555-555-5555')

It's handy if all of your keywords and values are already in a dictionary and you want to just pass them in.

One thing to note, you can't use keyword arguments (kwargs) before positional arguments (args). So you can't do call_me(**phone_dict, some_other_var) but you can do call_me(some_other_var, **phone_dict).

Thanks a lot. And yes, I forgot that the order of parameters is important. Just like a ParamArray in /VBA?/ (regular expressioning there, LOL), a dict parameter can appear in the function parameter list only after the positional arguments.