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

Getting no where with this, not understanding whats required with list required in output

Hi, I've tried numerous different ways but to no avail to get this to work, I don't understand what is the list required in the output

strings.py
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 string in dicts:
        strings =  string.format(name=strings['name'], food=strings['food'])
    return strings

print(string_factory(dicts, string))

4 Answers

Matthew Hill
Matthew Hill
7,799 Points

To answer your second question here is what the ** is doing:

def string_factory(dict_list, string):
    temp_list = []
    for dictionary in dict_list:
        temp_list.append(string.format(name=dictionary['name'], food=dictionary['food']))
    return temp_list

But I'm afraid that's the best I can explain it with my limited understanding, Matt

Matthew Hill
Matthew Hill
7,799 Points

Ooops, my mistake. As I said, it still gets me!

def string_factory(dict_list, string):
    temp_list = []
    for dictionary in dict_list:
        temp_list.append(string.format(**dictionary))
    return temp_list

The difference here is that I had specified the word dictionary for each dictionary in the list, so you need to continue using that name. Hence my update includes **dictionary now, the 'kwargs' is just a convention and isn't actually the important part, it's the double asterix that specifies you're unpacking a dictionary. Matt

Matthew Hill
Matthew Hill
7,799 Points

Hi John,

Sorry to hear that, I'll show you what I did and talk you through it. Don't worry, unpacking dictionaries is still a difficult topic for me too!

def string_factory(dict_list, string):
    temp_list = []
    for dictionary in dict_list:
        temp_list.append(string.format(**kwargs))
    return temp_list

You were quite close but look at the differences. First, my for loop on line 3 is for dictionary in dict_list:. Remember, they're not strings but a dictionary that is composed of keywords ('name' and 'food') and their corresponding values, which in this case are strings of names and types of foods.

Now, on my 4th line I am unpacking each dictionary into the format method. Remember, we're iterating through each dictionary in the list, so first time around we only have the first dictionary in the list ({'name': 'Michelangelo', 'food': 'PIZZA'}). This means that when we unpack it the keyword 'name' will be matched to the 'name' in the string, and it's value will be formatted into it, same for the keyword 'food' and its value.

This is then appended to our temporary list and the same is done for each dictionary in the list of dicts we are given. Hope this is a little clearer! Reply if you still have any questions, Matt

I used what you showed and all I get is' what happened to string_factory()' or 'kwargs not defined ' def string_factory(dict_list, string): temp_list = [] for dictionary in dict_list: temp_list.append(string.format(**kwargs)) return temp_list

Like I said can't seem to grasp it, if you use the format **, what follows it to put the values into string for each component of dicts. sorry for all the bother Matt John

Matthew Hill
Matthew Hill
7,799 Points

Hi John, seems like you've missed out two things in your function. First you need to be appending to your list, currently you're declaring it anew each time you loop through with your for string in dicts line. Secondly, they're not strings in dicts, they're dictionaries! Try using **string in your code (or whatever you change string to) to unpack those dictionaries instead of your long line with all the formatting.

Let me know if you still can't get it, Matt

Sorry Matthew I'm not understanding where your coming from, do I leave the strings list empty or declare it initially, really confused on this aspect , I understand the individual concepts but can't seem to link them together, getting really dis heartened at the moment.