Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Dictionaries String Formatting with Dictionaries

print() inside string_factory outputs what I would expect But when I return that same value it is off

the print() inside the function prints out four strings in a list with updated variables. Return only prints out one string. I'm missing something

def string_factory(dicts, string):

    index = 0
    for item in dicts:
        build_string = []
        # build_string = string.format(**dicts[index]) this didn't work
        index +=1
    return build_string

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}!"

output = string_factory(dicts, string)

1 Answer

Chris Howell
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,610 Points

Hey john larson

So I can't tell you how many times I have done this while coding by mistake because it is pretty easy to do.

First I will give you a hint and let you find it, if you still cant ill give and explain the answer.

for item in dicts:
        build_string = [] 

# HINT: These lines are causing a problem, still valid code. Just a logic error.

Thanks Chris, I'll take a look at that again. Each time through he loop, it looks like append is working. So I don't think that is the problem.. I tried this:

  • build_string = build_string.append(string.format(**dicts[index]))
  • but that broke the code.
  • I'm thinking I' missing something between print and return. Am I close?

Chris, I moved the empty build_string outside the loop and it SEEMS ti work correctly. I havn't passed it through the challenge yet, but I'm more concerned with just understanding how things work. I don't see why the empty array being inside the loop would make it print only the last line. Thanks

Chris Howell
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,610 Points

It should pass through the challenge if you have moved it above and outside the loop but still inside the function.

So what was happening was you were resetting the variable to a new empty list everytime until the last run through the loop.

build_string = [] # Made a new empty list
for item in dicts:
    build_string = [] # For every Item we loop over set this variable to a new EMPTY list.
    # Do stuff to the list

return build_string

Because the list is getting reset to a new empty list as it loops over each item, when you finally reach the end of that list. The LAST item doesnt have to loop again so it does not set the variable again. It breaks out of the loop and continues down to the next thing which is returning it.