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

Hello. I need help regarding the packing and unpacking lesson.

I need help regarding packing and unpacking... I have watched the video several times but sill do not understand the full concept of it

1 Answer

Steven Parker
Steven Parker
229,744 Points

I'll try explaining it. When you use the packing operator (also known as "splat") in a function definition, you're asking the system to take all the arguments passed to the function and gather them up into a single array. So, for example:

def someFunction(*packed)

Now if you call this function like this: "someFunction("first", "second", "third")", then inside the function you will have an array named "packed" which will have these contents: "[ "first", "second", "third" ]".

Does that help clear it up?

Hello Stevens. Sorry for the late answer. I would like to thank you for taking your time to help me with this lesson but unfortunately I still do not understand. I guess what gives me trouble is the purpose of packing and unpacking but also when to use it.

Steven Parker
Steven Parker
229,744 Points

One good time to use the packing operator is when you want to create a function that can take a variable number of arguments. You can use "len" to determine how many argument were given, and it's easy to construct a loop that uses the combined argument list as an iterator.l

Take another look at the examples in the videos and think about how you might write code to do the same job without using the "splat". That should help make it clear why it is so handy in those situations.