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 trialBijay Gurung
1,818 PointsDictionary Unpacking
def func(dict): return "My name is {}. I love {}".format(**dict)
How can I call this function??
1 Answer
Jeff Muday
Treehouse Moderator 28,720 PointsThe way you are looking at this with the **dict
tells me you already know something about programming! In Python, we typically use *args
and **kwargs
to make it clear about how we are intending to use the parameters (*args
, denotes a variable number of arguments, which are appended to a list, and **kwargs
for keyword/value pairs sent in as arguments.)
But the challenge is not quite as fancy, using named parameters in the FORMAT statement, so we must explicitly send name and food in the return statement. Another part of the challenge is realizing the dictionary type DOES NOT guarantee the named arguments are going to come in with a particular order, so... as you might expect,
What the challenge is looking for is this:
def favorite_food(dict):
return "Hi, I'm {name} and I love to eat {food}!".format(name=dict['name'], food=dict['food'])
A slightly 'safer' method to access a key/value pair
def favorite_food(dict):
return "Hi, I'm {name} and I love to eat {food}!".format(name=dict.get('name'), food=dict.get('food'))