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 (Retired) Dictionaries String Formatting with Dictionaries

Way off on the string_factory challenge?

I am a bit confused since we are dealing with a list of dictionaries, instead of a single dictionary. Not sure how that changes our approach but I have tried with no success. Anybody have a hint as to what I should focus on fixing here?

dicts = [
    {'name': 'Michelangelo',
     'food': 'PIZZA'},
    {'name': 'Garfield',
     'food': 'lasanga'},
    {'name': 'Walter',
     'food': 'pancakes'},
    {'name': 'Galactus',
     'food': 'worlds'}
]

string = "Hi, I'm {name} and I love to eat {food}!"

def string_factory(dicts,string):
  strings = []
  for item in dicts:
    string.format(name, food)
    strings.append(string)
  return strings

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You're close! Your for loop is going through the list of dictionaries, so each item in it is a dictionary. When you call .format(), you need to either pass keywords and the values from the dict (.format(name=item['name']) or pass the dictionary in with **.

Thanks Kenneth! I have been battling with this one throughout the day, even after reading your post but it finally just clicked because I wasn't appending the list correctly afterwards.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

nomadnick Way to go! Glad you solved it.

Have anything to share that I could change that might make easier/less confusing on future students?

I think I mostly got thrown off because in the video I think it was focused on dictionaries and then the challenge was based on a list of dictionaries, which I don't think I have had much exposure to. Maybe just a little further explanation to students on how that will change the way to view the problem. Either way, hopefully these posts will serve as clarification for someone else that hits a roadblock.

def string_factory(dicts,string):

  final_list = []

  for item in dicts:

    newString = string.format(**dicts[dicts.index(item)]

    final_list.append(newString)

  return final_list

Whats wrong with my function guys, please help!!!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You're missing a ) at the end of your string.format call.

You could just do string.format(**item), by the way, since item is a member of the dicts list.

Travis Bailey
Travis Bailey
13,675 Points

It didn't click for me until I visually started looking at the list of dictionaries like I was calling them by index. You're correctly looping through the list, but like Kenneth pointed out you need to change how you're passing the format function your data. Ironically if you look at the video you make the same mistake Kenneth made at first. I used ** as it's easier.

Thanks for your comment Travis. I finally worked it out. I was able to make sense of what you were saying but I was still returning incorrectly due to the append function not working the way I thought it was. Thanks for helping me out!