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

Name:GoogleSearch orJonathan Sum
Name:GoogleSearch orJonathan Sum
5,039 Points

Create a function named string_factory that accepts a list of dictionaries and a string. Return a new list build by usin

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):
    return_list=[]
    for intel in dicts:
        list1.append(string.format(**dicts))
        list1=return_list
    return return_list
#i know i need to change list1 to return_list, but i want to know why the code from above doesn't work?

Hey Jonathan,

Seems like you are trying to use

.format(**dicts)

when it should be

.format(**intel)

You might want to change the list1.append to return_list.append aswell as you've already created an empty list to append the strings to. Here's the modified version of your function with comments for you to refer to, you should be able to pass the code challenge with it.

def string_factory(dicts, string):
    return_list = []
    for intel in dicts:
        # old:
        # list1.append(string.format(**dicts))
        # new:
        return_list.append(string.format(**intel))
        # removed:
        # list1=return_list
    return return_list

2 Answers

Name:GoogleSearch orJonathan Sum
Name:GoogleSearch orJonathan Sum
5,039 Points

Micheal Allen That is what i am asking. I know i can pass it if i removed "list1=return_list and change list1 to return_list. I want to know why. Is that because it is in the for loop?

What you're doing in your version of the code is asking to format the whole list of dictionaries instead of single dictionary from the list. so swapping **dicts ( the list of dictionaries ) to **intel ( a single dictionary from the list of dictionaries) fixes the problem.

Name:GoogleSearch orJonathan Sum
Name:GoogleSearch orJonathan Sum
5,039 Points

Micheal Allen

That was my mistake. I have done this changlle more than 10 times. It is just i mistakly put the **dicts,instead of **intel. This is just a answer key from google search. I just don't know why i can't use these code. I guess that is because in the "for loop".

list1=return_list
    return return_list

Ahh I see what you mean, heres your code with comments on why:

def string_factory(dicts, string):

    return_list = []

    for intel in dicts:
        list1.append(string.format(**intel))
        # you are trying to append to a variable (list1) which hasn't been defined yet,
        # return_list.append(string.format(**intel)) would work as return_list has already
        # been defined outside of the loop and before the loop

        list1 = return_list
        # list1 will define itself as return_list every time
        # the 'for loop' loops ( 4 times in this case) 

    return return_list
    # return return_list  would be returning an empty list as return_list still  = []

Hope this helps