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

Rodman Frowert
Rodman Frowert
989 Points

Hit the wall here with packing/unpacking...

I don't understand how I need to complete this challenge. I wrote some quick code just to test unpacking a single dict into the function and that seems to work. But, I can't figure out how I'm supposed to unpack a list of dictionaries. Do the dictionaries need to be removed from the list first before being sent to the function?

This is the first challenge in the entire course that I can't wrap my head around what is happening and how I can go about fixing the problem. Lost on the packing/unpacking thing....

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

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

def string_factory(name, food):
    new_list = []
    template = "Hi, I'm {} and I love to eat {}!".format(name, food)
    # for name in names:
    #   new_list.append(template.format(**name))
    new_list.append(template.format(name, food))
    print(new_list)

string_factory(**{'name': 'rodman', 'food': 'pizza'})
# string_factory(**values)

4 Answers

Hey Rodman,

The for loop you wrote that's commented out is almost there. In Python, you can access the dictionaries within the values using 'key, value' (or whatever you want to call them) and enumerate(). See the example below.

template = "Hi, I'm {name} and I love to eat {food}!"
def string_factory(values):
    new_list = []
    for key, value in enumerate(values):
        new_list.append(template.format(**value))

    return new_list

I googled "python **unpacking" and this seemed like a good reference if you're having trouble.

https://hangar.runway7.net/python/packing-unpacking-arguments

Hope this helps and let me know how it goes!

Kurt

I made an edit to the template, I forgot to put name and food back into the brackets!

Patrick Madden
Patrick Madden
1,632 Points

I see that this works, but isn't the point of Team TreeHouse to teach us what is needed to pass the code challenges? Nowhere in the videos leading up to this challenge did it give us:

for key, value in enumerate(values):

Granted, I'm sure this is the correct way to do it and I can't argue because I am still new to all of this, but I have a feeling there is a different way it's wanting us to get the results based on what we have already learned. Please correct me if I'm wrong. If TreeHouse is not going to teach us what is needed for the code challenges, I feel like I may be better off not renewing my subscription and taking to learning Python from YouTube.

Rodman Frowert
Rodman Frowert
989 Points

Hey thanks for that Kurt. I'll give this a try in the morning and see how it goes. My brain needs a break!

Rodman Frowert
Rodman Frowert
989 Points

Kurt,

That was VERY helpful. It looks like this works now:

template = "Hi, I'm {name} and I love to eat {food}!"
values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]

def string_factory(values):
    new_list = []
    for key, value in enumerate(values):
        new_list.append(template.format(**value))
    return new_list

list_b = string_factory(values)
print(list_b) 

I'm wasn't familiar with enumerate() at all so I'll look that up further to see what is going on there. Also, part of the problem was I was calling the function with:

string_factory(**(values))

That doesn't work of course so that was throwing me off.

This is the 1st tutorial on Treehouse that I feel wasn't explained very well. Kenneth says that packing/unpacking commonly trips up people learning Python and he is right. I think they should have spent more than 6:00 minutes explaining this part of the language as it was totally foreign to me.

Oh I see how that could've been an issue. I'm glad you got it figured out though! And I'm with you on this tutorial; it could've used a little more detail to really drive the point.

Good luck!

seong lee
seong lee
4,503 Points

can someone explain this challenge to me? i figured it out but i still kinda don't get it.