 Welcome back everyone. This is Brian. We're going to talk about packing and unpacking data. Now, what are we really talking about here? Well, we have a problem with arg and keyword arg is that we cannot easily use list sets, tuples, and dictionaries. Instead, we have to pack and unpack the data. And that's what this video is really going to dive into. Let's take a look. So let's look at packing data. And this sounds like it's going to be super complex. But actually, in the spirit of Python, it's super, super easy. So I'm just going to make a function called pack. And we're going to make a parameter here called nums. And this is a star arg right here, which means one or more variables. And in here, we're going to say go ahead and print this out, just so we can see what we're actually getting. Now let's go ahead and call this pack. And I want to pack one, two, and three. Let's see what's actually going on here. Sure enough, it has made a tuple. From here, we can very easily say or add in nums and then do something like this. Get those individual numbers out of there and see exactly what's going on inside the tuple. Super, super simple to wrap your head around. This works as expected. However, there are some issues here. And let's go ahead and take a look at how we would unpack the data. Okay, packing data is, well, simple. And we've done this before. But what if we try to do the opposite and unpack the data? What are we really talking about here unpacking? So let's go ahead and make an example. Let's say def unpack. And let's give it three variables A, B and C. From here, I'm just going to print out unpack that way we can see what's going on, followed by print. And we can just grab those directly. Super simple, probably the simplest function will ever write, say B, C, and of course, line those up to B and C. Just looking at that function, it's pretty straightforward, pretty obvious what's going to happen. But now let's introduce a problem. I'm going to say num equal. And I want to make a list of numbers one comma two comma three. If we say unpack. And just give it our variable. What do you think is going to happen? Well, intelligence is already telling us there's going to be a problem here. No value for argument. And it says, Oh, see, missing two required optional, or, I'm sorry, positional arguments B and C. Wait, what? But I gave it a list. There's three elements in this list. What's really going on? Well, what's happening is we have to do this. To tell Python specifically, take this list and unpack it. And we do that just by adding a star right here. Let's try that again. I'm going to clear this out. And now it magically works. So what we're driving home here is when you see this asterisk, this little star, no matter what contest, whether you're using args or keyword args, or you're using it up here as a parameter, or you're using it as an argument. Think of this as telling Python, you're going to be packing or unpacking data and Python I want you to take care of all the messy details. So I don't have to. As with everything programming related, it's not that simple. Let's look at another issue. The dictionary issue. Let's go ahead and make a dictionary. So I'm going to say D equals dictionary. And I'm going to say name equals Ryan. Age equals 46. And that equals cat. Feel free to put in whatever values you want. You just need a dictionary with three elements. Now let's go ahead and say print and say packing dictionary. And let's go ahead and try to pack this. So we're going to call our pack function. And we're smart enough to know now that we need to put that asterisk there because we're going to tell Python you deal with the details. And we're going to put our dictionary there. And clear out our results down here and see what happens. Uh oh, we've got a problem. It's only getting the keys. It did get the keys successfully, but it's only getting the keys and remember our dictionary is a key value pair. So Oh, that is frustrating. Let's try the opposite. Let's try to unpack that. And let's go ahead and say unpacking. Andy little function name right there. And run again, unpack. And again, ABC, it's only getting the keys. So you're absolutely right. We have to do it a special way for dictionaries. Let's take a look. Looking at the dictionary issue, how it's only getting the keys, we have to treat the special. So we are going to look at an example of how to pack a dictionary. Now when you say pack a dictionary, I get this image of putting a dictionary in your backpack and going off to school. And that's kind of what we're doing here. So we're going to say define pack a dictionary. And let's go ahead and say we want to do asterisk, asterisk, nums. Notice the double asterisk, asterisk stands for wildcard. So we're telling it we're going to get two things. And if this looks eerily familiar, it's because we're talking about you guessed it keyword args. A dictionary is nothing more than a key value pair. So this is exactly what we're talking about keyword args. Let's go ahead and say print. And let's actually print this out. I want to see what we're being fed here from Python land, nums equals and then let's just write this out. Now let's go ahead and call this. And I want to say, let's go ahead and feed it some arbitrary data, just make up whatever you want. As long as you got three values, it doesn't really care. I'm just going to say name Brian, age equals 46. And pet equal. That's run this and sure enough, nums is equal to see these little squiggly lines a dictionary object. So it's automatically done this for us basically under the hood. What it's done is it's called the dict function and converted all those keyword arguments into a dictionary for us. From here, it is ridiculously simple to work with. I'm just gonna say print f. And let's go ahead and say, and we want actually we're going to do a for loop set of pulling these out one at a time. Or K in nums. Last minute change of plans here. So there is our keyword. And then we want to say equals and give it our key. There we go. Save run. Let's see what this looks like. Sure enough, act name, age and pet works as expected. As long as you have that double asterisks in there, you now have access to the entire object as a dictionary. Just to wrap this whole thing up, let's do the polar opposite. We are now going to unpack a dictionary. So let's go ahead and say D E F unpacked it. And boy, that's a funny name. I'm probably going to get some flack in the comments about that one. But the name, age and pet, we're just going to stick with that. And a little paradigm there. And let's go ahead and say print. We're going to unpack a dictionary object here. Let's go ahead and say print. And you guessed it, it is just ridiculously simple to do this. Do the magic copy and paste. I'm going to speed this up just to spend. All right, now to call this. Well, how would we actually go about calling this? We have a dictionary object out there. So let's go ahead and reuse that dictionary object. In case you're wondering, it's this guy right here. And I'll actually just grab him and bring it right here. Just recreate the wheel. It's already set, but I'm going to set it again just so we can see it on the screen. And we're going to unpack that dictionary object. Because we're using a dictionary, we need the double asterisks telling it, Hey, we are working under the hood with keyword args and watch this thing in action. So it's going to create a dictionary. And then we are going to tell Python unpack this into these. See, unpacking a dictionary named Brian age 46, cats. So this video. Well, seemingly simple, we're actually doing a lot of work under the hood. I should say Python's doing all the work for us. We are packing and unpacking data. We've given examples on how to pack and unpack. And unpack lists, sets and tuples and how to pack and unpack dictionary objects and some of the issues that arise. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. Drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become, the more I'll create and publish out on YouTube. Thank you for watching.