Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Dictionary Unpacking

Download

Sample Code (.zip)

4.7 KB
Download

Course Slides (PDF)

89.1 KB

Take the Quiz: Test your knowledge with our interactive “Python Dictionary Iteration” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Python Dictionary Iteration

Dictionaries are one of the most important and useful data structures in Python. Learning how to iterate through a Dictionary can help you solve a wide variety of programming problems in an efficient way. Test your understanding on how you can use them better!

00:00 Okay. In this last video of the series, I’m going to go over dictionary unpacking, which is a relatively new feature of Python that really makes it easy to iterate through and construct new dictionaries from other dictionaries.

00:13 So, coming back to our fruits and veggies dictionaries, which you’ll have seen before if you’ve watched some of the videos in this series already, we’ve got just some fruits and some vegetables with their prices.

00:25 Let’s take a look at this unpacking operator, which is going to be just two asterisks (**) in front of the name of your dictionary. So, as you can see, what I’m doing is I’ve put brackets around these two unpacked dictionaries with commas between them.

00:43 And what this does is it gives us one dictionary with the items of both previous dictionaries. So this is really convenient because what it allows us to do is it allows us to really easily iterate through. We can say, for example, for key, value in this mega-dictionary .items(): and we can just print, you know, our standard key goes to value. Ta-da!

01:13 So it’s a really simple way of going through here and iterating through multiple dictionaries. You can just unpack all of them and then you can just have access to the whole thing as one big dictionary.

01:25 And I just realized that our onions are incredibly expensive compared to everything else. That’s just my mistake by not putting a period in front of this .55.

01:33 But, you know, that’s okay. Onions—they deserve that high wage. So, this is a really cool way to merge multiple dictionaries into a new one, and you’ll notice that it’s very similar to what happens with ChainMap, but this dictionary unpacking you can actually use in a variety of other ways and that’s something you’ll have to get into in other video series.

01:53 But for example, you can pass them into functions. For example, in the definition of function you might see some function, and it has something like this, like **kwargs (keyword arguments).

02:04 And so this is a really cool way of using this dictionary unpacking operator as well, because it works into the basic functions of how Python works. But that’s a subject for another video.

02:15 What we’re using this for is in a similar way to how we used the collections ChainMap feature as a way to iterate through and perform operations on multiple dictionaries. Something to note, however, is that the behavior of dictionary unpacking, as opposed to ChainMap, is actually a little different when you have two of the same keys, the same key in each dictionary. So let’s say, for example, that I put in fruitsI’m going to put in fruits, I’m going to map 'onion'. Even though I know that’s wrong because it’s not a fruit, but I’m going to say fruits['onion'] = 1, right?

02:48 And then let’s see what happens if we do {**fruits, **veggies}. What happens when we merge these two? Well, in this case, 'onion' is .55, which is different in a ChainMap.

02:59 If we do a ChainMap, from collections import ChainMap, and we say ChainMap(fruits, veggies),

03:13 and then we say that ChainMap at 'onion',

03:18 we’ll get 1 here. And so a ChainMap, the value that comes out, is the leftmost value—the value in the leftmost dictionary. But in an unpacking,

03:30 it’s actually the rightmost value which takes precedence. So that’s an important distinction to note when you’re trying to use these two. So, that’s a really cool feature of Python—this dictionary unpacking operator.

03:41 It’s a very clean way to do it and it makes a lot of sense when you write it down. So, thank you for watching this video and the whole series. I hope you enjoyed.

03:48 Have a good one.

Avatar image for fjavanderspek

fjavanderspek on April 19, 2020

Great course, thanks!

Avatar image for renatoamreis

renatoamreis on May 3, 2020

Great course, thank you!!!

Avatar image for mikesult

mikesult on May 6, 2020

Thanks Liam for a great tutorial on working with dictionaries. I learned a lot of new things especially using ChainMap, the itertools and dict unpacking.

Avatar image for Liam Pulsifer

Liam Pulsifer RP Team on May 6, 2020

Thank you all for your kind comments! So glad I could help you learn some new things about Python dictionaries. :)

Avatar image for gabrielmachac

gabrielmachac on July 30, 2020

great tutorial, thank you

Avatar image for Sammy-Boy

Sammy-Boy on Sept. 3, 2020

Nice tutorial especially the chainMap and its applications . Thank you Liam!

Avatar image for Liam Pulsifer

Liam Pulsifer RP Team on Sept. 8, 2020

Glad you enjoyed the course @gabrielmachac and @Sammy-Boy! Best wishes as you continue on your Python-learning journey.

Avatar image for doglikebannock0d

doglikebannock0d on Jan. 27, 2023

I appreciate your guidance. I would also like to gain a deeper understanding of the optimal usage of various data structures, such as determining when it’s best to use dictionaries, lists or tuples in certain situations.

Avatar image for rosmi

rosmi on July 9, 2023

this was a super useful short course. Any chance it can get updated to include the ‘|’ and ‘|=’ commands from python 3.9? docs.python.org/3/whatsnew/3.9.html#dictionary-merge-update-operators

Avatar image for MischaelR

MischaelR on Jan. 27, 2024

Very good course. It covers a lot of interesting particularities of dictuonaries that are not often seen in a regular data analysis learning path.

Become a Member to join the conversation.