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 with "new_list"

I'll try to explain my problem.

I undertood I have to create a "new_list" where I can put in all the unpacked things that come from my dictionaires.

However, I tried to go to workspaces and print the "new_list" so I can see how it looks with all my dictionaires unpacked. Would it be something like:

new_list = ["key1"=1, "key2=2,...., "keyn=n"]?

Then, i'm not sure how to format the template. I would imagine it would be something lile:

template.format(new_list["name"],new_list[...])

is that right?

And then, do I have to loop through "new_list" to print as many dictionaires I have?

I'm really lost here.

Thanks in advance for your help!

Duarte

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}!"

def string_factory(list_of_dicts):
    new_list=[]
    for x in list_of_dicts:
        new_list.append(**d)
    return new_list

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you are close. the function needs to take a list of dicts, loop through them, and for each dict it needs to append the template filled in with the values from each dict to the list new_list, and return the list. the argument for append is the template with the format method called on it, and the argument for the format method is each dict (so your x looping variable) with the keyword args (two asterisks).