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

Michael Konstantinovsky
Michael Konstantinovsky
991 Points

How can I create a list with **?

Can't understand how to create a list Thanks

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!"]
listt = [{"name":"Shalev","food":"Meat"},{"name":"Eitan","food":"chips"}]

template = "Hi, I'm {name} and I love to eat {food}!"

def string_factory(listt):
    for key in listt:
        return template.format(**key)

string_factory(listt)

3 Answers

Michael Konstantinovsky
Michael Konstantinovsky
991 Points

Thanks a lot!!! You are right! It finally worked, I been confused with ** Great answer!

Yeah, its kinda tricky to understand but may be this would help you.

# 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(listt):
    retString = [];
    for key in listt:
        retString.append( template.format(name=key["name"], food=key['food']));
    return retString;

So get it done using '**' operator, we could use following code and then we don't have to specify what is "name" and "food" for the template.

def string_factory(listt): 
    ret_string = [] 
    for key in listt: 
        ret_string.append(template.format(**key) );
    return ret_string 
string_factory(listt);
Michael Konstantinovsky
Michael Konstantinovsky
991 Points

Thanks for an answer, but it still doesn't work. Here is my code:

listt = [{"name":"Shalev","food":"Meat"},{"name":"Eitan","food":"chips"}] template = "Hi, I'm {name} and I love to eat {food}!" def string_factory(listt): string = [] for key in listt: string.append(template.format(**key) return string string_factory(listt)

a little problem in syntax. Seems like you missed a parenthesis for append().

listt = [{"name":"Shalev","food":"Meat"},{"name":"Eitan","food":"chips"}] 
template = "Hi, I'm {name} and I love to eat {food}!" 
def string_factory(listt): 
    string = [] 
    for key in listt: 
        string.append(template.format(**key) );
    return string 
string_factory(listt);

Please do let me know of this doesn't works, thanks.

And another important trick for treehouse: use three backtics '`' followed by language name followed by the code to share code for that language. for more details see 'Markdown Cheatsheet'.