Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dictionaries Packing and Unpacking Dictionaries

Wilfried Allico
Wilfried Allico
2,495 Points

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
216,032 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?

Wilfried Allico
Wilfried Allico
2,495 Points

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
216,032 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.