In this python tutorial, we will discuss the Python concatenate dictionary and also we will cover these below topics:
- Python concatenate dictionaries
- Python concatenate dictionaries with the same keys
- Python concatenate dictionary key and value
- Python concatenate dictionary values
- Python concatenate dictionary value string
- Python concatenate dictionary of lists
- Python 3 concatenate dictionaries
- Python join dictionaries on key
- How to concatenate two dictionaries in python
- Merge multiple dictionaries python
- Python concatenate nested dictionaries
Table of Contents
Python concatenate dictionaries
Here, we can see how to concatenate dictionaries in python.
- In this example, I have taken two dictionaries. To concatenate the Python dictionaries .update is used.
- The .update() method is used to insert the specified items into the dictionary.
- To get the output, I have used print(dictionary1).
Example:
dictionary1 = { 'Pen': 5, 'Pencil': 4, 'Chocolate' : 15 } dictionary2 = {'Apple': 25,'Ball': 10,'Doll' : 20 } dictionary1.update(dictionary2) print(dictionary1)We can see the concatenated dictionary as the output. You can refer to the below screenshot for the output.
The above Python code we can use to concatenate dictionaries.
You may also like, Python program to print element in an array.
Python concatenate dictionaries with same keys
Here, we can see how to concatenate dictionaries with the same keys in python
- In this example, I have imported a module called Counter from collections. The Counter is a subclass used to count the key-value object.
- The counter holds the data in an unordered collection just like a hashable object.
- To concatenate the dictionary, I have used dictionary = dictionary1 + dictionary2.
- To get the output, I have used print(“dictionary”, str(dictionary)).
Example:
from collections import Counter dictionary1 = Counter({'kitkat': 10, 'MilkyBar': 5, 'diarymilk' : 50, 'munch' : 15}) dictionary2 = Counter({'kitkat' : 7, 'diarymilk' : 5, }) dictionary = dictionary1 + dictionary2 print("dictionary", str(dictionary))We can see the concatenated string as the output. You can refer to the below screenshot for concatenate dictionaries with the same keys in Python.
The above code we can use to concatenate dictionaries with same keys in python.
Python concatenate dictionary key and value
Here, we can see how to concatenate dictionary key and value in python.
- In this example, I have taken a dictionary with key and value. 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.
- The map() method is used in mapping the key and value and it holds the list of each iteration.
Example:
dictionary = {'Name': 'Kushi', 'Class': 'LKG'} new_dict= ','.join(map(','.join, dictionary.items())) print(new_dict)To get the output, I have used print(new_dict). The key and value pair are concatenated as the output. The below screenshot shows the output.
This is how to concatenate Python dictionary keys and values.
Read Python Dictionary index
Python concatenate dictionary values
Here, we can see how to concatenate dictionary values in python.
- In this example, I have taken a dictionary. To convert the value into string str() is used.
- Only to concatenate the value of the dictionary .value() method is used.
- The .value() is the inbuilt method that returns the value of the dictionary.
Example:
dictionary = {"orange" : 9, "Apple" : 5, "mango" : 25} print("dictionary is : " + str(dictionary)) result = (dictionary.values()) print(result)To get the output, I have used print(result). We can see only values are concatenated as the output. The below screenshot shows the output.
This is how to concatenate dictionary values in Python.
Python concatenate dictionary value string
Here, we can see how to concatenate dictionary value string in python.
- In this example, I have taken two dictionaries such as dictionary1, dictionary2.
- To concatenate the value string dictionary .get() is used. The .get() method returns the value for the specified key in the dictionary.
- The for loop is used for iteration, to get the value in the string format str() method is used.
Example:
dictionary1 = {'fruits' : 'Apple', 'vegetable' : 'Carrot'} dictionary2 = {'fruits' : 'Mango', 'vegetable' : 'Tomato'} result = {key: dictionary1[key] + dictionary2.get(key, '') for key in dictionary1.keys()} print("The string dictionary is : " + str(result))The string values of the dictionary are concatenated as the output. The below screenshot shows the output.
Python concatenate dictionary of lists
Here, we can see how to concatenate dictionary of lists in python.
- In this example, I have taken a dictionary, and the values of the dictionary are assigned in the list.
- To concatenate the value that is present in the list, an empty list is created and the values are stored in the created list by using .value().
Example:
dict = {"pen" : [2, 3], "pencil" : [2, 4], "book" : [5,3]} print("The original dictionary is : " + str(dict)) res = sum(dict.values(), []) print("The list values are : " + str(res))To get the output, I have used print (“The list values are : ” + str(res)). The below screenshot shows the output as the value of the dictionary stored in the list created.
This is how to concatenate dictionary of lists in Python.
Python 3 concatenate dictionaries
Here, we can see how to 3 concatenate dictionaries in python.
- In this example, I have taken three dictionaries as dictionary1,dictionary2,dictionary3.
- To concatenate the three dictionaries ** is used. The** is used to pass multiple arguments to the function directly using a dictionary.
Example:
dictionary1 = {'orange' : 9, 'Apple' : 5, 'mango' : 25} dictionary2 ={'kitkat': 10, 'MilkyBar': 5, 'diarymilk' : 50, 'munch' : 15} dictionary3 = {'Apple': 25,'Ball': 10,'Doll' : 20 } dictionary4 = {**dictionary1, **dictionary2, **dictionary3} print(dictionary4)To get the output, I have used print(dictionary4). The below screenshot shows the output.
This is how to concatenate dictionaries in Python 3.
Python join dictionaries on key
Here, we can see how to join dictionaries on key in python.
- In this example, I have taken two dictionaries as dictionary1,dictionary2. To join the dictionaries on the key, I have used for loop as for k in dictionary1.
- To get the output, I have used print(dictionary3)
Example:
dictionary1 = {1:1,2:2,3:3} dictionary2 = {1:11,2:12,3:13} dictionary3 = dict([(k,[dictionary1[k],dictionary2[k]]) for k in dictionary1]) print(dictionary3)We can see the values of the same key are joined to concatenate. You can refer to the below screenshot for the output.
This is how to join dictionaries on key in Python.
How to concatenate two dictionaries in python
Now, we can see how to concatenate two dictionaries in python.
- In this example, I have taken two dictionaries. To concatenate two dictionaries, I have used the .update() method.
- The .update() method inserts the specified items into the dictionary.
- To get the output, I have used print(dictionary1).
Example:
dictionary1 = { 'Pen': 5, 'Pencil': 4, 'Chocolate' : 15 } dictionary2 = {'Apple': 25,'Ball': 10,'Doll' : 20 } dictionary1.update(dictionary2) print(dictionary1)We can see the concatenated dictionary as the output. You can refer to the below screenshot for concatenate two dictionaries in python.
The above code, we can use to concatenate two dictionaries in python.
Merge multiple dictionaries python
Here, we can see how to merge multiple dictionaries in python.
- In this example, I have defined a function as def Merge and passed the two dictionaries as the parameter. To merge the dictionary I have used **.
- The ** is used to pass multiple arguments to the function directly using a dictionary.
- And then the function is returned, To merge the dictionaries, I have used Merge(dictionary1, dictionary2).
- To get the output, I have used print(dictionary).
Example:
def Merge(dictionary1, dictionary2): result = {**dictionary1, **dictionary2} return result dictionary1 = {'mobile': 10, 'laptop': 8} dictionary2 = {'remote': 4, 'charger': 5} dictionary = Merge(dictionary1, dictionary2) print(dictionary)To get the output, I have used print(dictionary). You can see the concatenated dictionary as the output. The below screenshot shows the output for merge multiple dictionaries python.
This is how to merge multiple dictionaries in python.
Python concatenate nested dictionaries
Here, we can see how to concatenate nested dictionaries in python.
- In this example, I have taken two nested dictionaries as dict1,dict2.
- To concatenate the dictionary, I have used dict1.update(dict2).
- The .update() method is used to insert the specified items into the dictionary.
- To get the output, I have used print(dict1).
Example:
from collections import Counter dictionary1 = Counter({'kitkat': 10, 'MilkyBar': 5, 'diarymilk' : 50, 'munch' : 15}) dictionary2 = Counter({'kitkat' : 7, 'diarymilk' : 5, }) dictionary = dictionary1 + dictionary2 print("dictionary", str(dictionary))0You can refer to the below screenshot for the output for concatenate nested dictionaries in Python.
This is how to concatenate nested dictionaries in Python.
You may like the following Python tutorials:
- Indexing and slicing in Python
- Python concatenate arrays
- Python Tkinter drag and drop
- Python intersection of sets
- Python read a file line by line example
- Create and modify PDF file in Python
- Create a game using Python Pygame
In this tutorial, we have learned about Python concatenate dictionaries, and also we have covered these topics:
- Python concatenate dictionaries
- Python concatenate dictionaries with the same keys
- Python concatenate dictionary key and value
- Python concatenate dictionary values
- Python concatenate dictionary value string
- Python concatenate dictionary of lists
- Python 3 concatenate dictionaries
- Python join dictionaries on key
- How to concatenate two dictionaries in python
- Merge multiple dictionaries python
- Python concatenate nested dictionaries
Bijay Kumar
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.