Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dictionaries String Formatting with Dictionaries

How to unpack dict in function and then use values from it in .format()?

I found some answers from a year ago, but it appears the problem has changed enough that it doesn't quite work.

In the video, he only explained how to use the values if you used the ** in calling the function, not how to do it in the function.

Thanks

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

1 Answer

Øyvind Andreassen
Øyvind Andreassen
16,839 Points

Hi!

I would recommend re-reading the question, and think about what they are asking you to do. The question is to create a function which returns a formatted string based on a dict as the input.

What's in the file when you load it.

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

The input dict looks like this we can assume based on the names in the return statement.

{
"name" : "Kenneth",
"food" :  "tacos"
} 

I'm going to unpack what your function does, and explain why it fails. Please read this or rewatch the videos, before just copy the solution.

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

You are first of all creating a list new_list and then appending values to it. But look at the function, the template variable you are trying to append to the list is after the append statement. This will throw an error in your code. Also remember that a function is done when it returns something. You are also trying to format the string with variables that is not defined, food and name.

You need to assign the keys of the dictionary to the correct , so the correct way of doing this would be:

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

the lesson was about packing and unpacking dicts - I still have no idea how to unpack this dict within the function - I get the idea of better calling the values with with keys correctly. But I still can't call those without unpacking the dict - and the lesson didn't cover how to do that in this way - I will look for a different teacher and lesson elsewhere on the net I guess - or maybe it will be covered in a future lesson?

I did rewatch it before posting the question.