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

Having a hard time understanding what the question is asking for...packing/unpacking!?

So I am trying to solve the string_factory challenge. Here is the code I came up with, which seems to work for the template given. However, I don't understand why or where to use ** in this code to solve this challenge. The challenge is not accepting my answer.

Where am I going wrong?

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


def string_factory(diclist):
    newlist = [] #new empty list
    for dic in diclist: #iterating through the list so that I operate on each ditionary one step at a time

        newlist.append("Hi, I'm {name} and I love to eat {food}".format(name=dic["name"],food=dic["food"])) #append the strings in the lists

    print(newlist)
    return newlist



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

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

When we unpack a dictionary, it's turned into key=value pairs. So **{"name": "Garfield"} would be turned into name="Garfield".

Now, looking at your code, where are you using keyword arguments?

I'm not using keyword arguments. The challenge was not accepting my answer because I forgot a stupid little exclamation mark at the end of the string! Haha! I thought that we needed to use keyword arguments to solve this, which was the source of my confusion. It's all good now. Thank you Kenneth for your response!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

In the code you posted:

newlist.append("Hi, I'm {name} and I love to eat {food}".format(name=dic["name"],food=dic["food"])) #append the strings in the lists

That .format(name=dic["name"]) bit...name=dic["name"] is a keyword argument :)

Hey Kenneth - I posed on a different post regarding disemvowel in hopes that you'd reply. My solution seems to be working perfectly fine, but the challenge is not accepting it.

Here is my code for that:

vowels = ['a','e','i','o','u']

def disemvowel(word):
    word = word.lower()
    wordlist = list(word) #converting string into list
    wordlistcopy = wordlist[:] #copy of my original list

    for letter in wordlist:
        for vowel in vowels:
            try:
                wordlistcopy.remove(vowel) #removing from copy while looping through original 
            except ValueError:
                pass


    word = ''.join(wordlistcopy) #convert list back into string
    print(word)
    return word
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Were you told to lowercase the word? Also, remember, list.remove() only removes the first instance it finds of the particular member, so you have to either remove multiple copies multiple times or find a better solution.

So the code I posted works perfectly fine, even with using list.remove() [Yes, it only moves the first instance, but I made the logic so that works to my favor]. BUT my problem was returning the lower case of the passed string! All fixed! Thank you kindly! :D