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 trialharpreet grewal
19,018 Pointsmy answer kinda comes out right but its obviously wrong. what am doing wrong?
i get back two lists but when it comes back the second time the answer is right. i know this is not the right way but its the only way i can figure out how to pass an list of dictionaries.
# 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}!"
dict = []
def string_factory(name=None, food=None):
dict.append("Hi, I'm {} and I love to eat {}!".format(name, food))
print(dict)
return dict
huh= [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]
for a in huh:
string_factory(**a)
3 Answers
markmneimneh
14,132 PointsHello
here is way to do it ...
def string_factory(a_dict_list):
dict = []
for item in a_dict_list:
name = item['name']
food = item['food']
dict.append("Hi, I'm {} and I love to eat {}!".format(name, food))
return dict
please review and even though this will pass, see if you can do same differently.
james malloy
7,959 PointsHi Harpreet,
if it helps, I 've just tried this one and did it as follows
values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]
template = "Hi, I'm {name} and I love to eat {food}!"
def string_factory(dict_list):
ret_list = []
for kwargs in dict_list:
ret_list.append(template.format(**kwargs))
return ret_list
harpreet grewal
19,018 Pointsi had an answer similar to this but i thought the question wanted us to use "**". Thats where i was stuck
"Return a new list of strings made by using ** for each dictionary in the list and the template string provided."
agzagi
2,399 PointsYes, the challenge for me was as well using the **kwargs. The normal string.format() is something we have already done several times in this course and not the challenge here.
The answer from James Malloy below this one helped me out.