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

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

having trouble with strings and dictionaries

when i run this, it keeps saying 'dicts' is a list and not a map. what is it that i should do to solve this problem?

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):
    return string.format(**dicts)
string_factory(dicts, string)

4 Answers

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

hi Ryan, i managed to tweek my code a bit. but it still giving me an error, now it say "what happened to 'string_factory'"

icts = [ {'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}!"

string_list = [] def string_fatory(dicts, string): for item in dicts: string_list.append(item) string.format(**string_list) return string_factory

Ryan S
Ryan S
27,275 Points

Hi Fhatuwani,

It's hard to tell from the format of your code but I think you are pretty close. You have the right idea but there are just a couple things to fix. First, you will need to define string_list inside the function, but before the for loop. And second, you will need to format the string in the append() function, but you will be unpacking the item, not the string_list. Remember that the item, in this case, is a dictionary that is contained within a list.

Try the following:

def string_factory(dicts, string):
    string_list = []
    for item in dicts:
        string_list.append(string.format(**item))
    return string_list
Ryan S
Ryan S
27,275 Points

Hi Fhatuwani,

The variable dicts is a list that contains a bunch of dictionaries, so in your return statement you are trying to unpack a list, not a dictionary. Another thing is that the challenge is asking you to return a list of formatted strings, not a single formatted string. So you will need to define an empty list, string_list = [], for example, and append that list in a for loop. You'd then return string_list when the looping is done.

Remember that in the for loop you will be looping through an individual dictionary in the dicts list, and will need to unpack each dictionary.

Also, you don't need to call the function at the end. The code challenge will do that for you when you submit.

Hope this helps,

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

thank you so much Ryan, i tried it and it worked!!!!!