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

Enzo Cnop
Enzo Cnop
5,157 Points

How does new_list get automatically populated? How does this function work on a step-by-step basis?

I don't understand how the new_list gets 'filled', or how python knows to fill it. I've been stuck on this challenge for weeks and I'm only passing it by copy and pasting. Maybe a step by step breakdown of the code will help me understand it for myself.

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

dict_list = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]

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

def string_factory(dict_list):
    new_list = []
    for item in dict_list:
        new_list.append(template.format(**item))
    return new_list

2 Answers

Stuart Wright
Stuart Wright
41,120 Points

Ok, so you are looping through a list of dictionaries. This means that on the first run through the loop, item is equal to:

{"name": "Michelangelo", "food": "PIZZA"}

Then you pass that item to the format method of the template string using:

template.format(**item)

I assume this is the part that's causing the confusion. Passing **item to a function is known as dictionary unpacking. It is a way to pass keyword arguments to a function. Because the placeholders in your template are named ({name} and {food}), the corresponding values in your dictionary of "Michelangelo" and "PIZZA" are inserted into the string. This only works because the keys of your dictionary match up to the placeholder names. After that the new item is appended to your new_list and returned, which I assume you are fine with.

Here's a simpler example of dictionary unpacking, using a basic function which simply adds two numbers together:

def add(x, y):
    print(x + y)

You can pass a dictionary to this function, as long as it has exactly two keys, x and y:

my_dict = {'x': 1, 'y': 2}
add(**my_dict)

The result of the above would be 3, because 1 is passed as x, and 2 is passed as y.

Enzo Cnop
Enzo Cnop
5,157 Points

Thanks for your reply, it helped me figure out the root of my confusion, which lies in a much more basic principle. I'm confused by how variable names are assigned in for loops. For example:

course_minutes = {"Python Basics": 232, "Django Basics": 237, "Flask Basics": 189, "Java Basics": 133}

for i in course_minutes:
    print(i)

The way that 'i' attaches to the key value pairs in course_minutes feels backwards/arbitrary and trips me up a lot. I guess I've answered my own question, but the confusion from this principle pops up all over the place. I know it's meant for ease of use, but as this is the first programming language I'm trying to learn some of the shortcuts feel more confusing than useful.

Shreyas Papinwar
Shreyas Papinwar
2,371 Points

My advice to you is re watch packing and unpacking dictionary video again, and do some of your own challenges offline or in workspaces.

It will surely help you.

GOOD LUCK!