Merging Dictionaries - Python Tips and Tricks #13
Skills:
AI Pair Programming80%
Key Takeaways
This video demonstrates how to merge dictionaries in Python using the update method, keyword arguments operator, and dictionary unpacking, including a discussion on Python 3.9's merging operator and update operator.
Full Transcript
[Music] what is going on guys welcome back to python tips and tricks tutorial series in today's video we're going to learn about merging dictionaries in python so let us get right into it now in python we have multiple ways to merge dictionaries let's say we have a dictionary 1 and this dictionary has the key a and a has the value 1 for example and then we have the key b and b has the value seven for example and we have a second dictionary big two and here we also have the key b but with a different value let's say four and then we have c and some other number eight so those are the two dictionaries and we want to merge them together into one final dictionary and this dictionary should either be a1 b7 c8 or a1 b4 c8 depending on which b we're going to take uh sometimes we can we can decide by by stating the right order and sometimes it's just random we're going to talk about that in a second and the first way that we're going to learn about um when it comes to merging dictionaries is the so-called update method now before we get into that let me show you that it's not as simple as just saying dick one plus dick two this would work maybe in libraries like numpy but it doesn't work in core python we cannot use the operand type for for plus for addition with dictionaries so we cannot just add dictionaries together so what we do instead is we say dict1 dot update dig two what happens then is we take dictionary one and then update it with the values keys and values from dictionary two which means that first of all of course we add the c that is missing in dictionary one but we also update the value of b so instead of 7 b is now going to be 4 since we updated with the second dictionary um actually we would need to print that and also let me just clear the console so print dictionary one as you can see we get b4 now of course i can do it the other way around as well i can say dictionary two dot update and then i can just update it using dictionary one but then of course i have to print dictionary two and we get uh b7 now the order is not that important we have keys we have values it doesn't matter which uh position uh at which position these keys are these key value pairs are the only problem or actually not a problem but the only thing that you need to keep in mind with this method is that you're actually changing dictionary one and two you cannot say dictionary three equals dictionary one dot update so it doesn't work like that you cannot just uh combine them and then return a new dictionary you actually change dictionary one or dictionary two so uh this is one thing that you might not want you might wanna keep dictionary one and dictionary two the way they are but still combine them into a new separate dictionary and this does not work with the update method now if we actually want to merge these two dictionaries and create a third one a new one we can actually use the keyword arguments operator and this is essentially just two stars and we can use those two stars this keywords argument operator to pass dictionaries into a dictionary so it would work somewhat like that you would say dig 3 or dick merge whatever and then you would create a dictionary but instead of passing key value pairs you pass dictionary one and two but you don't just pass them like this dick one two two because that would not work let's see what happens if we run that uh we get unhashable type dict um this is because we cannot just pass whole dictionaries what we need to pass here are key value pairs in order to get the key value pairs in order to be able to pass dictionary or the key value pairs off the dictionary we need to use the keyword arguments operator so those two stars here and here which essentially means we're creating a new dictionary with the items of dictionary one and the items of dictionary two essentially and then let's go ahead and print print deck three to see the results for that a quick console clear and there you go you can see we have a1 b4 c8 uh now if you want to have the other order you can just swap these two because the first one gets added first and the second one gets added last obviously or a second which means that if you first add dictionary one you get b7 but then you add dictionary two so you're basically updating it so to say and you end up with uh b equals four or the key is b and the value is four so if you want to reverse that you just say dictionary two dictionary one and then you will end up with b equals seven so uh you can do it like that and this is how you merge dictionaries using the keywords argument operator so last but not least i want to show you a method that is very modern very new and it's only available in python 3.9 or above or newer versions basically it is not possible in python 3.8 it's not possible in python 3.7 3.6 or something only in python 3.9 or above the only problem is i'm not running python 3.9 so i'm not going to be able to execute the script however the code is very simple you can do it on your computer if you have python 3.9 installed if not i'm going to show you a similar way that you can use but this is actually the most modern way it's very simple because python 3.9 introduced a merging operator so what we need to do here is we just say dig three equals and now the only thing that we need to do in order to merge dictionary one in dictionary two is we say dig one merging operator pick two that's it in python 3.9 we would get the exact same result that we had um before now when i run this in python i'm not sure what i'm running here 3.8 or 3.7 when i run this i get dictionary and dictionary is not possible for this operator however there's also another operator in python 3.9 that we can use and this is the update operator so instead of using the update method we can use the update operator and it's essentially dict 1. again the merging operator with the equal sign dick 2. so this would be dict 1 update dig 2. in python 3.9 we cannot use that here so even if i comment that out this is also not possible here actually because it's an invalid syntax in general so it's not only not applicable to dictionaries it doesn't even know what that is so those are the python 3.9 things and now i'm going to show you a method that also works in all the python versions uh uses the same operator but in a different way we essentially just say dig three equals and now we say dict so we type casting and we're type casting the items from dictionary one merged with the items from dictionary two now the interesting thing about that is that we have no clue what dictionary we're going to end up with so if we do that everything can happen i'm going to clear the screen here i'm going to run this and you can see now we have b7 um instead of b4 but now i can run this again we have b4 we have b4 we have b7 it's every time random like there are certain things in computer science uh especially when you when you're into c program into c programming you know that you cannot be sure what's going to happen with some statements and this is one of those you don't know what dictionary you're going to end up with it's basically random or you just have to to go and see what happens so it's undefined so to say um if you don't have a problem with that you can use that otherwise i would use either update or the keyword argument operator or use python nine with those new operators here so that's it for today's video hope you enjoyed it hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below also make sure you subscribe to this channel in order to see more future videos for free and if you don't want to miss any video click on the notification bell and activate the notifications for this channel other than that thank you very much for watching see you next video and bye [Music] you
Original Description
In today's video we learn about different ways to merge dictionaries in Python.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
🐍 The Python Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
I Bought 128GB of Unified Memory to Run Local Coding Models. Here’s What Actually Worked.
Medium · LLM
Companies Still Running Coding Interviews Will Be the Losers of the AI Era
Medium · Data Science
Pre-allocate Like a Pro
Dev.to · Sukhpinder Singh
The Real Divide in the AI Economy Isn't Human vs. Machine
Hackernoon
🎓
Tutor Explanation
DeepCamp AI