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 56,648 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!!