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 (2016, retired 2019) Dictionaries String Formatting with Dictionaries

joshua korir
joshua korir
1,445 Points

i did not understand how to unpack a dictionary, i have rewatched the video multiple times with no success

unpacking dictionaries, P.S. I have continued to the later parts of the series and everything is quite doable but the strategy i have been using is to put all the keys or values in a list and manipulate there. is this the right approach or am i missing some useful dict manipulation techniques by coercing everything to lists

string_factory.py
def unpack (name=None,food = None):

    return  

def favorite_food(dict):

    return "Hi, I'm {name} and I love to eat {food}!".format(name,food)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The ** notation can be hard to grasp. The double-asterisk ** means "*convert a dictionary object in key/value pair assignments. For example:

adict = {'a': 1, 'b': 2}
#  **adict is then replaced with a=1, b=2
print("a is {a} and b is {b}".format(**adict))
# is the same as
print("a is {a} and b is {b}".format(a=adict['a'], b=adict['b'])

In the case of a parameter list such as def my_func(**kwargs):, this means to expand the single dictionary object pass as an argument into separate key/value pair assigments.

Post back if you need more help. Good luck!!