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

Manpreet Singh
Manpreet Singh
13,014 Points

Please help me to solve this python question

I am not able to understand question want from me to do

string_factory.py
# Example:
# values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]
# string_factory(values)
# ["Hi, I'm Michelangelo and I love to eat PIZZA!", "Hi, I'm Garfield and I love to eat lasagna!"]

template = "Hi, I'm {name} and I love to eat {food}!"

1 Answer

Hi there!

Don't worry, you're not alone, load of us got confused by the wording of this question, including myself - it comes up all the time!

It's so confusing I think because there's a few different ways you could achieve the desired outputs, but it really wants to you use dictionary unpacking as shown in the previous video, and what you have to actually do gets a bit lost in the process!

If you have a look at the comments at the top of the string_factory.py, it's actually much clearer what it's asking:

you'll get a list that looks like this as input:

input_list = [
    {"name": "Michelangelo", "food": "PIZZA"}, 
    {"name": "Garfield", "food": "lasagna"}
]

And your function needs to go through each item in the list (the dictionaries) inserting their name and food values into the template string so that your function returns a list like this:

output_list = [
    "Hi, I'm Michelangelo and I love to eat PIZZA!",
    "Hi, I'm Garfield and I love to eat lasagna!"
]

You're really supposed to use ** to unpack the dictionaries, so don't be tempted to use

for key, value in dict.items()

Hope it helps!