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

Caleb Shook
Caleb Shook
3,194 Points

I 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.

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):
    print()

1 Answer

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

just 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
Caleb Shook
3,194 Points

Thank 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.