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 Packing and Unpacking Dictionaries

unpacking challenge

Sorry, but I've watched these videos again and again. I am very confused on several points:

  1. Are we meant to keep {name} and {food} within the return()? Because this is not what is shown in the runup video.

  2. do we keep "dict" in the func() parameters. Are we not meant to use **kwargs?

Is this all meant to be in one function? Do we need a separate function to pack the dictionary?

Please if you can help me out with some kind of work flow. I just have no idea how this should take place and between the video and what is given with the instructions, I find this has not set one up to do understand this well

Sebastiaan van Vugt
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Sebastiaan van Vugt
Python Development Techdegree Graduate 13,554 Points

Hi. I also struggled after merely watching the video. The teacher's notes are helpful in this case however. The following points may help understand this exercise:

  • Variables like "name" and "food" can be passed on to printed as explained in the exercise
  • The variables are stored in the dictionary where they are called keys
  • packing uses **kwargs and unpacking **[dictionary name] (without the square brackets and the name of your dictionary)

The fun thing now is that the variables that cannot be read at first will become readable when Python is told to look in your dictionary. See the teacher's notes for an example:

my_dict = {'name': 'Kenneth'} >>> "Hi, my name is {name}!".format(**my_dict) "Hi, my name is Kenneth!"

def packing(**kwargs): ... print(len(kwargs)) >>> packing(name="Kenneth") 1

Therefore you just need to unpack the dictionary in this exercise and you're done :)

4 Answers

Bryan Land
Bryan Land
10,306 Points

I spent a good amount of time trying to understand this concept as well and it wasn't until I watched this video which explains the args(*) and kwargs(**) operators as they pertain to packing and unpacking in functions.

With dictionaries, there are no indices, but there are keys, and when you call for instance the packer() function, you pass in a variable and automatically set it to be something like brand='Merriam Webster', where 'brand' is the key and 'Merriam Webster' is the value, because if you pass in a single variable you will get will get a TypeError...

def packer(**kwargs):
    print(kwargs)

packer(brand='Merriam Webster')
# output is...
>>> {'brand': 'Merriam Webster'}

So it makes sense to say that when you need to extract the values inside a dictionary to use in another function (like format()), all you need to do is specify the ** operator with the dictionary that you pass into THAT function, like so:

def favorite_food(dict):
    return "Hi, I'm {name} and I love to eat {food}!".format(**dict)
# I'm going to call it with a dictionary so it makes more sense
favorite_food({'name': 'Bryan', 'food': 'Gumbo'}
# output...
>>> "Hi, I'm Bryan and I love to eat Gumbo!"

I hope this clears it up! Good luck!

Thanks. I was stuck and the video wasn't explained very good. Now I understand better. :)

hey Bryan that was a huge help - I can't believe the answer to the challenge was to just type in **dict

the lesson doesn't really make it clear that the ** is like a command that actually does the packing and unpacking of a dictionary. or at least says "hey, I'm receiving a dictionary as an argument - which you can use the contents of the dictionary without manually pulling the pieces out by just calling it as **dict (or whatever the argument is) and we'll take care of the rest"

thanks brian , they are not really explaining well in the videos about certain concepts ,and then they ask you now in the objectives , about the poorly and inadequately explained concepts, and then you get stuck