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

don't know what to do next

Hi guys I don't know what to do next. I'm not understanding how will I get the values printed in the template string

Hope you can help me,

string_factory.py
# Example:
values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]


def string_factory(values):
    new = list(**)
    return new

# 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 Tiago

Basically, the challenge wants you to loop over each item in the list, because each item in the list is a dictionary it can be unpacked using double asterix i.e **, since the string 'template' has the keys of the dictionary you can unpack it like so.

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

hope this helps