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 trialCaleb Shook
3,194 PointsI have no idea where to begin.
I need to create a function that will return a list of strings made from a function that takes a dictionary from a provided list. I have no idea how to access the dictionary from the list.
# 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):
print()
1 Answer
james south
Front End Web Development Techdegree Graduate 33,271 Pointsjust loop through the list like any other. each item is a dictionary, and thus you can use the usual dictionary methods like items() on each element in the list.
for i in myListOfDicts:
for j in i:
for instance would loop through the keys of each dict in the list.
Caleb Shook
3,194 PointsCaleb Shook
3,194 PointsThank you for the quick and helpful response! For some reason I was just stuck on the concept of a list made of dictionaries, but that was very helpful.