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

Joshua Dam
Joshua Dam
7,148 Points

What is a mapping? etc.

Since it's asking for a new list of strings I made an empty list titled new_list.

I made the function title string_factory that accepts the argument with the name list_of_dicts.

The list of dicts (including keys and values) is names values (which was already created).

I thought I should then take each item within that list and append it to the new_list which is empty.

After that, I can return the new_list with each item appended.

It says append() argument after ** must be a mapping, not a list. I really don't understand what I'm doing when it comes to dictionaries and packing/unpacking

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!"]

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

new_list = []

def string_factory(list_of_dicts):
    for items in values:
        new_list.append(**values)
    return new_list

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

a mapping is a dictionary, so it throws that error because you have a list of dicts after the asterisks. you are pretty close, you just need to tweak a few things. i got this to work without adding any lines of code. firstly, list_of_dicts is your parameter, so that's what you use inside of the function body, not values. values is the argument you pass in when calling the function. secondly, you are not using the template in the function. that is what you append to new_list, while calling the format method on template. to fill in the blanks of the template, you call your loop variable, items, with the keyword args (two asterisks) as the argument to the format method.