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

Sahil Kolwankar
Sahil Kolwankar
3,393 Points

How do I unpack the dictionary?

I'm stuck in the following quiz:

"Complete the favorite_food function below. It accepts a dictionary as an argument. Your function should unpack that dictionary and pass it to the format method as keywords, then return the resulting string."

I think I don't understand the question that well. Can someone help me with this?

string_factory.py
def favorite_food(name, food):
    return "Hi, I'm {name} and I love to eat {food}!".format(dict["name"], dict["food"])

favorite_food(**dict)

2 Answers

Steven Parker
Steven Parker
230,995 Points

It looks like you have the right idea, but instead of calling the function and using the "splat" operator in the argument, the challenge wants you to do the dictionary unpacking inside the function as part of calling "format".

You won't need to call the function yourself anyway, the validator does that.

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

Taig Mac Carthy
Taig Mac Carthy
8,139 Points

Thanks Rotem, seriously: thank you VERY much. I was having a hard time.

Btw: when did Kenneth explain that you can unpack a dictionary at the same time as calling it with .format() and that inserting the name of the keys inside {} gets the values placed? That's two things that no one has explained so far in the course, neither Kenneth nor Greg, right? Why do they torture us like that?