Python3 New Features | New Dictionary Operations in Python 3 – features of python

Hello friends in this Article we are going to learn some cool new features in Python 3.9 are the dict merge operator and the dict update operator, If you are new to this Article please subscribe to this channel and if you like this  Article please share this with your friends let’s move on to my laptop and starts today’s tutorial.

Python3 New Features | New Dictionary Operations in Python 3

Hello everybody today we will going to see some new feature in python 3 versions especially the   dictionary merge and update operator there are two new operators we will going to see on to this   so this is my blog that is posted on my website i am going through this onto this Article so first   let’s see what are the things we are going to learn into this tutorial Article we will explore   two new operators introduced in python 3.9 that is dictionary merge operator and dictionary update   operator so let’s going on to this new operator let’s quickly recap what is [dict] in python.

so dictionaries are python’s implementation of a data structure that is more generally known as an   associative array a dictionary consists of a collection of key-value pairs each key-value   couple maps the key to its associated value in-built dict class used to represent the   dictionary data type in python for example we can see here that key one and value one these   two are this is one key-value here and another key value here is this one and this totally enclosed   by curly braces so key and values are separated by colon and the pair of key values are separated   by comma and total data is enclosed by curly braces we write into this way the dictionary one.

so let’s go through the first one the dictionary merge operator this is a new operator that’s that   is available from 3.9 and this is a new feature that is added onto the 3.9 it’s not available up   to three point eight okay let’s this operator can merge two or more than two dict object and provide   a new dict object with all the key and value pairs present in the provided dictionaries that means if   we have two or three dictionaries and dictionaries have different key-value pairs and we want to   collapse all those key-value pairs and create a new dict then this new dictionary merge operator   will help to gain that so the symbol of the dictionary merge the operator is a pipe this pipe   symbol is used as an operator let’s understand the operation of this operator with a suitable example.

suppose we have two different dictionaries named d1 and d2 which are assigned below for example   here d1 I have taken a dictionary that is named human viratkohali and location Rajkot   name is the key and location is another key with some values and i am printing this dictionary and   this is we we see that here name is viratkohali and location is Rajkot another dictionary that is   d2 that’s only one thing that is contact and this is my contact number so if I print this there is   only key that is contact and with the value of this contact string this operator is new to us   but previously like before introducing with this operator i used a method to do the same thing and   that method is like using kw args so I used the star-star onto this dictionary and merge into   one so that’s why I wrote here previously we used the star-star kwargs method to generate a new dict   object by merging two dict objects the example code is written below in the code section so   star-star d1 and comma star-star d2 this enclosed by curly braces, it also does the same thing that’s.

Also Read This 👉   how to use sum function in python?

it’s merging d1 and d2 and it its provide a new dictionary with all those key values like   name location and contact i am getting into this dictionary the new generated dictionary   this method is quite fixation or you can say quite difficult to understand but we will get the same   result by using the newly introduced operator by python 3.9 that is the dict merge and python uses   the pipe symbol as an operator for dict merge so here we can see that is a very simple d1 pipe   d2 it’s also doing the same thing but remember this is available from python 3.9 onwards   here the new operator pipeline is handy to use and easy to understand   right so also please comment in the comment section if you think that this operator is really   helpful for you for the operations especially for why when you use this for the merge or which one   you would like to use the old one or the new one please let me know in the comment section.

what will be the output if d1 and d2 having the same key with a different value let’s   see so before answering this question let’s understand the operator and operand in the below   statement here d1 pipe d2 we can see here three things is here d1 pipe and d2 so here this pipe.

this is the operator and d1 here it’s a operand d2 is as well as d2 is also an operand but d1   is the left operand and d2 is the right operand if d1 and d2 have the same key for example key 1   and we applied the dict merge operator on d1 and d2 like below so we merged d1 and d2.

like using this pipe then what will be the resultant of this dict where d1 having name sorry   where d1 having key one key two and d2 also have been key1 and key3 so key one is the common factor on   d1 dict as well as d2 direct also so when we applied this d1 pipe d2 that is d1 dict merge d2.

we are getting that key one’s value is from d2 that’s why that’s because it’s on the right.

Also Read This 👉   How To Implement Leap Year Program in python?

the side that means right operand when I applied d2 pipe d1 at that time we are getting key1’s value as  which has the on d1 that’s because it’s on the right why it’s happening because the rightmost   one is updating the values of existing key present on the on to that date which is generating we also   can use multiple uses of the dict operator on a single python statement that means in   a single line it’s possible to merge more than two dictionary objects like this one d1 d2 not d3 it’s   a dictionary object over here written over here so we can also do this by using pipe so we can merge   more than two dictionaries in a single statement next another new operator that is on python 3.9   that is dictionary update operator and this is pipe equals to as we use the plus operator with   plus equals to similarly it’s we are using pipe equals to so if we already have a dictionary.

we are using in this example d1 is a dictionary with key-value pairs with q1 value 1 from d1 and   k2 value 2 from d1 and d2 also have q1 value 1 from d2 and e3 that is value 3 from d2 and we are   doing a d1 pipe equals to d2 at that time we are getting the result q1 equals to value 1 from d2 k2   value 2 from d1 and k3 is value 3 from d2 that means the output of d1 pipe d2 is stored   on d1 so that’s why it’s called dictionary update operator the above statement will update the left   operand dict d1 with the key values of the right operand d2 python dick class also have a method   called update and that’s act the same as this dict update operator in python 3.9 but dict update   operator that is pipe equals to is faster than the update method that is present onto the date class.

hopefully, this tutorial helps you to understand the “features of python” as well as new operator in python 3.9 questions for you is   that’s which which one you would like to use or do the same thing for merging two dict objects the old one or the new one hey thank you for reading this tutorial Article if you really like this Article   please hit the like button and don’t forget to subscribe to this channel also share this Article with   your friends and colleagues and all those who are trying to learn python thank you have a good day