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 (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'.