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 Introducing Dictionaries Iterating and Packing with Dictionaries Packing with Dictionaries

Stheven Cabral
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Stheven Cabral
Full Stack JavaScript Techdegree Graduate 29,854 Points

How do you pack a dictionary into a function?

In the 'Packing with Dictionaries' video, the instructor packs a sequence of variables and arguments (i.e. name='Doug') into a function using **kwargs. The title of the video is misleading as it doesn't actually pass a dictionary into the function. How do you actually pass each element of a dictionary into a function?

pet
pet
10,908 Points

Just so you know, you can't pack a dictionary into a function. Also the video title just is Packing with Dictionaries and if you read the description it says "Learn how to pack with dictionaries." Hope this helps. :)

1 Answer

To unpack a dictionary into a function, you first must define the dictionary with the needed parameter(s) for function as key-value pairs i.e.

example_dictionary = {
    'name' : 'user',
    'role': 'pythonista'
}


def example_print_info_for(name, role):
    print(f"My name is {name}, and I am a {role}")

you then use the double asterisks (**) before the dictionary name when calling the function i.e.

example_print_info_for(**example_dictionary)

You should get the output

My name is user, and I am a pythonista

Thanks for this. I had been thinking that I had to use *args or **kwargs exactly until I read this. I thought it would make more sense this way and was just waiting to see it used as you did.