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

Deniz Kahriman
Deniz Kahriman
8,538 Points

Unpacking question - tried writing the code based on the previous examples in the video and got stuck. Thanks in advance

Watched the previous video a few times and still don't understand how to approach this question and what I'm doing wrong. Thank you very much in advance!

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_of_dicts):
    for dict in list_of_dicts:
        string_factory(template.format(dict))
        return list_of_dicts

3 Answers

Steven Parker
Steven Parker
229,732 Points

Here's a few hints:

  • you probably don't want to call your function from inside your function
  • you want to build a new list of strings to return, start by creating a new empty list
  • the argument coming in is already a list, you don't need the unpacking operator there
  • but you might want to do unpacking of the individual dictionaries as you format them
  • the return is indented too far, right now it's inside the loop
  • don't name a variable "dict", that's a reserved word
  • finally, return the new list of strings (not the original list of dicts)
Deniz Kahriman
Deniz Kahriman
8,538 Points

Thank you, Steven! I tried again and again (with the limited knowledge I have) and I'm still stuck :( This is what I came up with based on your hints above:

def string_factory(list_of_dicts): new = [] for dictionary in list_of_dicts: new = template.format(**dictionary) return new

Steven Parker
Steven Parker
229,732 Points

That's a bit tough to read without formatting! But assuming it's formatted correctly, the issue would be the assignment of "new" inside the loop. You set it up to be a list, but when you assign it with a string it's no longer a list. Plus, that wipes out any previously stored values.

So instead of an assignment, you need to do something that would add the string being created onto the list without affecting what's already there. Do you know a way to do that?

And when posting code, be sure to use the instructions for formatting in the Markdown Cheatsheet found below the "Add an Answer" area. :arrow_heading_down:

Deniz Kahriman
Deniz Kahriman
8,538 Points

Thanks, Steven! I tried "append" a few times but that didn't work neither. Hope the code looks fine based on the markdown cheatsheet.

template = "Hi, I'm {name} and I love to eat {food}!"
def string_factory(list_of_dicts):
    new = []
    for dictionary in list_if_dicts:
        new.append(template.format(**dictionary))
    return new
          ```
Steven Parker
Steven Parker
229,732 Points

You just have a typo now.

You wrote "list_if_dicts" (with an "i") instead of "list_of_dicts" (with an "o").

Deniz Kahriman
Deniz Kahriman
8,538 Points

Thank you so much, Steve!!!