Merging Dictionaries - Python Tips and Tricks #13

NeuralNine · Beginner ·💻 AI-Assisted Coding ·5y ago

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 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to merge dictionaries in Python using different methods, including the update method and keyword arguments operator, and discusses the implications of each approach.

Key Takeaways
  1. Create two dictionaries to merge
  2. Use the update method to merge dictionaries
  3. Utilize the keyword arguments operator to merge dictionaries
  4. Explore Python 3.9's merging operator and update operator
  5. Compare the results of different merging methods
💡 The choice of dictionary merging method in Python depends on the desired outcome and the version of Python being used.

Related Reads

📰
I Bought 128GB of Unified Memory to Run Local Coding Models. Here’s What Actually Worked.
Learn how to set up a local coding model on a MacBook Pro M5 with 128GB of unified memory for AI-assisted coding
Medium · LLM
📰
Companies Still Running Coding Interviews Will Be the Losers of the AI Era
Companies that still rely on traditional coding interviews will be left behind in the AI era, as AI-generated code becomes increasingly prevalent
Medium · Data Science
📰
Pre-allocate Like a Pro
Learn how to pre-allocate memory like a pro in C# 15 for efficient coding
Dev.to · Sukhpinder Singh
📰
The Real Divide in the AI Economy Isn't Human vs. Machine
Developers who combine AI with strong engineering fundamentals will thrive in the AI economy, rather than being replaced by machines
Hackernoon
Up next
Claude Code Local Google Ads: Automate Everything ($730K Earned)
Jono Catliff
Watch →