Python Python Collections Dictionaries String Formatting with Dictionaries

joshua korir
joshua korir
1,381 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
def unpack (name=None,food = None):


def favorite_food(dict):

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

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 55,471 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!!