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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

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.

vedavyas on Dec. 26, 2019

This course helped me to understand the itertools and it how to use them. Last four sections helped me alot.

Liam Pulsifer RP Team on Dec. 26, 2019

Great to hear, @vedavyas! Please do let me know if you have any lingering questions.

Pakorn on Dec. 27, 2019

Great tutorial, Thanks

bmorton on Dec. 28, 2019

Very helpful, clearly explained the concepts and how to apply them. Do you have a recommendation on where to find practice problems for the concepts tackled in these courses?

muzixaba on Dec. 29, 2019

Fantastic tutorial. I learned a lot, especially on using itertools with dictionaries.

Liam Pulsifer RP Team on Dec. 29, 2019

@Pakorn glad you enjoyed it!

Liam Pulsifer RP Team on Dec. 29, 2019

@bmorton, thanks for your kind words. As to practice problems, I would say just looking around on Google for “Python dictionary practice problems” will lead you to some good results.

Liam Pulsifer RP Team on Dec. 29, 2019

@muzixaba good to hear!

sion on Dec. 29, 2019

Great presentation. Useful stuff that was new to me, also pointers to other areas to look into. I liked you delivering it ‘warts and all’. It is always helpful to see mistakes and their redemption. Many thanks.

stephenm on Dec. 30, 2019

Very clear presentation of dictionary features and techniques. Many thanks.

dthomas01 on Jan. 4, 2020

This was really impressive. Learned a lot! Good work, Liam!

sergiossc on Jan. 5, 2020

Great job, Liam! These tools are very useful. Thx! ;)

datateam on Jan. 13, 2020

I am a data engineer so work with dictionaries a fair bit. Although I was familiar with a lot of the concepts, I still picked up a number of new things, like ChainMap! And a brilliant use case for popitem! Loved the presenting skills and the material was very valuable. Will look to see if I can apply some of the learnings to my job and in fun challenges :) Great work Liam!

Mallesham Yamulla on Jan. 27, 2020

Thanks for creating this tutorial, and again learned new things on dictionaries such as chainmaps, itertools, etc.

tobenary on Feb. 29, 2020

Thanks for the help in this course. What I do like to see at the end, is a complex ( dict inside dict ) and the iteration that is needed to be done. ( with error free ) ANYHOW, I learned a lot.

fjavanderspek on April 19, 2020

Great course, thanks!

renatoamreis on May 3, 2020

Great course, thank you!!!

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.

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. :)

gabrielmachac on July 30, 2020

great tutorial, thank you

Sammy-Boy on Sept. 3, 2020

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

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.

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.

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

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.