Python concatenate dictionary key and value
In this article we will discuss different ways to merge two or more dictionaries. Also, handle scenarios where we need to keep the values of common keys instead of overwriting them. In Python, the Dictionary class provides a function update() i.e. It accepts an another
dictionary or an Iterable object (collection of key value pairs) as argument. Then merges the contents of this passed dictionary or Iterable in the current dictionary. Let’s use this update() function to merge two dictionaries. Suppose we have two dictionaries i.e. Both dictionaries has a common key ‘Sam’ with different values. Now let’s merge the contents of dict2 in dict1 i.e. Now the content of dict1 is, All the
elements in dict2 are added to dict1. Keys which are common in both the dictionaries will contain the values as in dict2. Basically the dictionary we are passing in update() as argument will override the common key’s values. Therefore ‘Sam’ has value 20 now. Advertisements Another important point to notice is that, we didn’t got a new dictionary. The contents of dict1 changed and now apart from its existing contents it has the contents of dict2 too. What if we want to merged the contents of 2 or dictionaries to a new dictionary ? Let’s see how to do that. Merge two or more Dictionaries using **kwargs**kwargsUsing **kwargs we can send variable length key-value pairs to a function. When we apply ** to a dictionary, then it expands the contents in dictionary as a collection of key value pairs. dict1 = { 'Ritika': 5, 'Sam': 7, 'John' : 10 } When we apply ** to this dictionary, it de-serializes the contents of dictionary to a collection of key/value pairs i.e. **dict1 is equivalent to, 'Ritika': 5, 'Sam': 7, 'John' : 10 So, let’s use **kwargs to merge two or more dictionaries. # Create first dictionary dict1 = { 'Ritika': 5, 'Sam': 7, 'John' : 10 } # Create second dictionary dict2 = {'Aadi': 8,'Sam': 20,'Mark' : 11 } Now merge the contents of dict1 and dict2 to a new dictionary dict3 i.e. # Merge contents of dict2 and dict1 to dict3 dict3 = {**dict1 , **dict2} print('Dictionary 3 :') print(dict3) Content of the new dictionary is, Dictionary 3 : {'Ritika': 5, 'Sam': 20, 'John': 10, 'Aadi': 8, 'Mark': 11} How did it worked ? **dict1 & **dict2 expanded the contents of both the dictionaries to a collection of key value pairs i.e. dict3 = {'Ritika': 5, 'Sam': 7, 'John' : 10 , 'Aadi': 8,'Sam': 20,'Mark' : 11} Therefore, a new dictionary is created that contains the data from both the dictionaries. Both dict1 & dict2 had one common key ‘Sam’. In dict3 value for this common key ‘Sam’ is as in dict2 because we passed the **dict2 as second argument. Merge three dictionariesSimilarly we can merge 3 dictionaries i.e. # Create first dictionary dict1 = { 'Ritika': 5, 'Sam': 7, 'John' : 10 } # Create second dictionary dict2 = {'Aadi': 8,'Sam': 20,'Mark' : 11 } # Create second dictionary dict3 = {'Mark': 18,'Rose': 22,'Wong' : 22 } # Merge contents of dict3, dict2 and dict1 to dict4 dict4 = {**dict1, **dict2, **dict3} print('Dictionary 3 :') print(dict4) Output: Dictionary 4 : {'Ritika': 5, 'Sam': 20, 'John': 10, 'Aadi': 8, 'Mark': 18, 'Rose': 22, 'Wong': 22} Till now we have seen that while merging dictionaries, values of common keys are getting overridden. What if we want to keep all the values ? Merge two dictionaries and add values of common keysSuppose we have two dictionaries with common key i.e. # Create first dictionary dict1 = { 'Ritika': 5, 'Sam': 7, 'John' : 10 } # Create second dictionary dict2 = {'Aadi': 8,'Sam': 20,'Mark' : 11 } Now we want to merge these dictionaries in way that it should keep all the values for common keys in a list i.e. def mergeDict(dict1, dict2): ''' Merge dictionaries and keep values of common keys in list''' dict3 = {**dict1, **dict2} for key, value in dict3.items(): if key in dict1 and key in dict2: dict3[key] = [value , dict1[key]] return dict3 # Merge dictionaries and add values of common keys in a list dict3 = mergeDict(dict1, dict2) print('Dictionary 3 :') print(dict3) Output: {'Ritika': 5, 'Sam': [20, 7], 'John': 10, 'Aadi': 8, 'Mark': 11} Both the dictionaries had a common key ‘Sam’. In the merged dictionary dict3, both the values of ‘Sam’ from dict1 & dict2 are merged to a list. We can use this function to merge 3 dictionaries and keep the all the values for common keys i.e. # Create first dictionary dict1 = { 'Ritika': 5, 'Sam': 7, 'John' : 10 } # Create second dictionary dict2 = {'Aadi': 8,'Sam': 20,'Mark' : 11 } # Third Dictionary dict3 = {'Mark': 18, 'Rose': 22, 'Wong': 22} # Merge 3 dictionary and keep values of common keys in a list finalDict = mergeDict(dict3, mergeDict(dict1, dict2)) print('Final Dictionary :') print(finalDict) Output: Final Dictionary : {'Mark': [11, 18], 'Rose': 22, 'Wong': 22, 'Ritika': 5, 'Sam': [20, 7], 'John': 10, 'Aadi': 8} Python Dictionary Tutorial - Series:
Subscribe with us to join a list of 2000+ programmers and get latest tips & tutorials at your inbox through our weekly newsletter.Complete example is as follows : def mergeDict(dict1, dict2): ''' Merge dictionaries and keep values of common keys in list''' dict3 = {**dict1, **dict2} for key, value in dict3.items(): if key in dict1 and key in dict2: dict3[key] = [value , dict1[key]] return dict3 def main(): # Create first dictionary dict1 = { 'Ritika': 5, 'Sam': 7, 'John' : 10 } # Create second dictionary dict2 = {'Aadi': 8,'Sam': 20,'Mark' : 11 } print('Dictionary 1 :') print(dict1) print('Dictionary 2 :') print(dict2) print('*** Merge two dictionaries using update() ***') # Merge contents of dict2 in dict1 dict1.update(dict2) print('Updated dictionary 1 :') print(dict1) print('*** Merge two dictionaries using ** trick ***') # Create first dictionary dict1 = { 'Ritika': 5, 'Sam': 7, 'John' : 10 } # Create second dictionary dict2 = {'Aadi': 8,'Sam': 20,'Mark' : 11 } # Merge contents of dict2 and dict1 to dict3 dict3 = {**dict1 , **dict2} print('Dictionary 3 :') print(dict3) print('*** Merge 3 dictionaries using ** trick ***') # Create second dictionary dict3 = {'Mark': 18,'Rose': 22,'Wong' : 22 } # Merge contents of dict3, dict2 and dict1 to dict4 dict4 = {**dict1, **dict2, **dict3} print('Dictionary 4 :') print(dict4) print('*** Merge two dictionaries and add values of common keys ***') # Create second dictionary # Merge contents of dict2 and dict1 to dict3 print(dict1) print(dict2) # Merge dictionaries and add values of common keys in a list dict3 = mergeDict(dict1, dict2) print('Dictionary 3 :') print(dict3) dict3 = {'Mark': 18, 'Rose': 22, 'Wong': 22} print(dict3) # Merge 3 dictionary and keep values of common keys in a list finalDict = mergeDict(dict3, mergeDict(dict1, dict2)) print('Final Dictionary :') print(finalDict) if __name__ == '__main__': main() Output: Dictionary 1 : {'Ritika': 5, 'Sam': 7, 'John': 10} Dictionary 2 : {'Aadi': 8, 'Sam': 20, 'Mark': 11} *** Merge two dictionaries using update() *** Updated dictionary 1 : {'Ritika': 5, 'Sam': 20, 'John': 10, 'Aadi': 8, 'Mark': 11} *** Merge two dictionaries using ** trick *** Dictionary 3 : {'Ritika': 5, 'Sam': 20, 'John': 10, 'Aadi': 8, 'Mark': 11} *** Merge 3 dictionaries using ** trick *** Dictionary 4 : {'Ritika': 5, 'Sam': 20, 'John': 10, 'Aadi': 8, 'Mark': 18, 'Rose': 22, 'Wong': 22} *** Merge two dictionaries and add values of common keys *** {'Ritika': 5, 'Sam': 7, 'John': 10} {'Aadi': 8, 'Sam': 20, 'Mark': 11} Dictionary 3 : {'Ritika': 5, 'Sam': [20, 7], 'John': 10, 'Aadi': 8, 'Mark': 11} {'Mark': 18, 'Rose': 22, 'Wong': 22} Final Dictionary : {'Mark': [11, 18], 'Rose': 22, 'Wong': 22, 'Ritika': 5, 'Sam': [20, 7], 'John': 10, 'Aadi': 8} How do you combine keys and values in Python?Below are the eight standard methods by which you can merge two dictionaries in python.. 1) Using update() method.. 2) Using merge(|) operator.. 3) Using ** operator.. 4) Unpacking the second dictionary.. 5) Using collection.ChainMap() method.. 6) Using itertools. ... . 7) Using dictionary comprehension.. 8) Add values of common keys.. How do you concatenate a dictionary key?To concatenate the key and value of the dictionary . join is used and ',' separator is also used. The . items() method returns the view object that returns an object containing the key-value pair.
How do I concatenate a string in a dictionary Python?Method #2 : Using Counter() + “+” operator
In this, the Counter function converts the dictionary in the form in which the plus operator can perform the task of concatenation.
Can we concatenate dictionaries in Python?In the latest update of python now we can use “|” operator to merge two dictionaries. It is a very convenient method to merge dictionaries.
|