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

Nikki Wong
Nikki Wong
9,066 Points

I am sorry but I have no idea what the question is asking! I don't understand how to implement the **..

This is the question:

Let's test unpacking dictionaries in keyword arguments. You've used the string .format() method before to fill in blank placeholders. If you give the placeholder a name, though, like in template below, you fill it in through keyword arguments to .format(), like this: template.format(name="Kenneth", food="tacos") Write a function named string_factory that accepts a list of dictionaries as an argument. Return a new list of strings made by using ** for each dictionary in the list and the template string provided.

I feel so clueless at this whole ** because I though it just makes dictionaries... but if what's being passed into the function is a dictionary then what's the purpose? plus using it inside the function to make a list? I'm so confused. Any help would be appreciated 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!"]

def string_factory(value):

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

    new_list = []

    for item in value:
        new_list.append(template.format(item["name"], item["food"])

    return new_list

template = "Hi, I'm {name} and I love to eat {food}!"
Francis Wanyonyi
Francis Wanyonyi
6,768 Points

Hi Nikki,

The question is asking you to create a list of strings, based on the template, each with a different person and food they love to eat. The examples of the input values and final string are in the comment section of the code. Dictionary unpacking takes one dictionary in values list at a time and unpacks it for you as a key and value so you don't need to do any assigning. i.e

template.format(**item) == template.format('name'='Michelangelo', 'food'='PIZZA')

Otherwise the code below will work. Hope this helps you and all the best.

# 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(list_o_dict):

    list_of_strings = []

    for item in list_o_dict:
        string = template.format(**item)
        list_of_strings.append(string)

    return list_of_strings

1 Answer

Nikki Wong
Nikki Wong
9,066 Points

Thank you so much francis! That really cleared things up!! Makes much more sense now!