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

Waleed Aljarman
PLUS
Waleed Aljarman
Courses Plus Student 947 Points

using dictionaries

To be honest, i didn't understand what the question needs or how to do it.

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!"]
def string_factory(**new_list):
    return(new_list)
new_list = [{'name':'james', 'food':'pizza', 'age':18}]

template = "Hi, I'm {name} and I love to eat {food}!"
Sneha Nagpaul
Sneha Nagpaul
10,124 Points

template.format(**new_list[0])

is

"Hi, I'm james and I love to eat pizza!"

I think this idea just needs to be incorporated in string_factory so it returns a list of these strings. Hope this helped.

1 Answer

You have to create an empty list for the new strings to go into. You are making this from a list of dicts that you cannot see as there are many more entries in the list of dicts than what they are showing you so just act like you don't know how many there are. So your for statement will be for each dictionary in list_of_dicts or what you put in as the arg for string_factory. Then you just append the list using template.format inside of the () you put after the .append and fill the .format part with **dictionary or **dict should also work. Then you just return the list you created to hold all of this. I had a hard time with this the first time. There are better ways to do it but not at this level.