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

Nick Meier
Nick Meier
1,905 Points

Is the solution to "String Formatting with Dictionaries" really that simple?

So I was really confused about the challenge task, and I struggled with comprehending what was asked from me, so I searched the forum and found some very complex solutions (to me at least they were), which confused me even more. After spending some more time over this and rewatching the belonging videos, I "came up" with this. Is this a valid way? and if so, why are there so many people taking a way more complex approach?

string_factory.py
def favorite_food(dict):
    return "Hi, I'm {name} and I love to eat {food}!".format(**(dict))

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Your solution is perfectly valid and very elegant. As far as other people coming up with more "complex" approaches, I would bet that some worked and some did not. But I also believe this comes down to human psychology. I believe that the instinct is to over-complicate a problem rather than over-simplify a problem.

One of the beautiful things about programming is that there are often multiple ways to solve any given problem. Some will be more efficient and some will be less efficient. As you move along, you will begin to get a feel for these.

I would say that for the time being, you should worry less about others' solutions and more about your own. And yours is just beautiful :smiley:

Hope this helps! :sparkles:

Nick Meier
Nick Meier
1,905 Points

That is very nice of you, thanks for the advice!

Istvan Nonn
Istvan Nonn
2,092 Points

Hi Nick Meier, The solution is that simple. It is valid because of the two asterisk "**".

The double asterisk form of kwargs is used to pass a keyworded, variable-length argument dictionary to a function. Again, the two asterisks () are the important element here, as the word kwargs is conventionally used, though not enforced by the language. Like *args, **kwargs can take however many arguments you would like to supply to it. However, **kwargs differs from *args in that you will need to assign keywords.

The difference between you and others is that you took the time to understand the problem and learn the right way. You have a very good problem solving approach and you can benefit from this.

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Agree with the other answers and I think this is the beauty of Python and why the deeper I get the more of a fan I become.

While your code is perfectly valid, just for fun you can even remove 2 more characters and reduce it by a little bit :smile:

# your solution
def favorite_food(dict):
    return "Hi, I'm {name} and I love to eat {food}!".format(**(dict))
# another valid solution - don't need parenthesis around dict
def favorite_food(dict):
    return "Hi, I'm {name} and I love to eat {food}!".format(**dict)
Nick Meier
Nick Meier
1,905 Points

It's actually good to know that those parenthesis aren't even needed; thanks for sharing!