How to concatenate 3 dictionaries in python


When it is required to concatenate two dictionaries into a single entity, the ‘update’ method can be used.

A dictionary is a ‘key-value’ pair.

Below is a demonstration for the same −

Example

 Live Demo

my_dict_1 = {'J':12,'W':22}
my_dict_2 = {'M':67}
print("The first dictionary is :")
print(my_dict_1)
print("The second dictionary is :")
print(my_dict_2)
my_dict_1.update(my_dict_2)
print("The concatenated dictionary is :")
print(my_dict_1)

Output

The first dictionary is :
{'J': 12, 'W': 22}
The second dictionary is :
{'M': 67}
The concatenated dictionary is :
{'J': 12, 'W': 22, 'M': 67}

Explanation

Two dictionaries are defined, and are displayed on the console. 

  • The ‘update’ method is called on the first dictionary by passing second dictionary as parameter. 
  • This would help concatenate the dictionary. 
  • This is displayed on the console.

How to concatenate 3 dictionaries in python

Updated on 12-Mar-2021 12:44:07

  • Related Questions & Answers
  • Python program to merge two Dictionaries
  • C# program to merge two Dictionaries
  • How to concatenate two files into a new file using Python?
  • C# program to merge two sorted arrays into one
  • Java Program to Concatenate Two Arrays
  • C++ Program to Concatenate Two Strings
  • Python program to merge to dictionaries.
  • Python - Intersect two dictionaries through keys
  • How can we merge two Python dictionaries?
  • Python - Difference in keys of two dictionaries
  • How to concatenate two strings in Python?
  • C# program to find Union of two or more Dictionaries
  • How do we compare two dictionaries in Python?
  • How to merge two Python dictionaries in a single expression?
  • Concatenate two lists element-wise in Python

This is a Python Program to concatenate two dictionaries into one dictionary.

Problem Description

The program takes two dictionaries and concatenates them into one dictionary.

Problem Solution

1. Declare and initialize two dictionaries with some key-value pairs
2. Use the update() function to add the key-value pair from the second dictionary to the first dictionary.
3. Print the final dictionary.
4. Exit.

Program/Source Code

Here is source code of the Python Program to add a key-value pair to a dictionary. The program output is also shown below.

d1={'A':1,'B':2}
d2={'C':3}
d1.update(d2)
print("Concatenated dictionary is:")
print(d1)

Program Explanation

1. User must enter declare and initialize two dictionaries with a few key-value pairs and store it in separate variables.
2. The update() function is used to add the key-value pair from the second to the first dictionary.
3. The final updated dictionary is printed.

Runtime Test Cases

 
Case 1:
Concatenated dictionary is:
{'A': 1, 'C': 3, 'B': 2}

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Next Steps:

  • Get Free Certificate of Merit in Python Programming
  • Participate in Python Programming Certification Contest
  • Become a Top Ranker in Python Programming
  • Take Python Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

How to concatenate 3 dictionaries in python

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

Sometimes, while working with dictionaries, we might have a problem in which we have lists as it’s value and wish to have it cumulatively in single list by concatenation. This problem can occur in web development domain. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using sum() + values() This is the most recommended method and one liner to perform this task. In this, we access all list values using values() and concatenation utility is performed using sum(). 

Python3

test_dict = {"Gfg" : [4, 5], "is" : [6, 8], "best" : [10]}

print("The original dictionary is : " + str(test_dict))

res = sum(test_dict.values(), [])

print("The Concatenated list values are : " + str(res))

Output : 

The original dictionary is : {'Gfg': [4, 5], 'best': [10], 'is': [6, 8]}
The Concatenated list values are : [4, 5, 10, 6, 8]

Method #2 : Using chain() + * operator This task can also be performed using the combination of these methods. In these, we just use inbuilt function of chain for concatenation to list and * operator is used to access all the list values into one. 

Python3

from itertools import chain

test_dict = {"Gfg" : [4, 5], "is" : [6, 8], "best" : [10]}

print("The original dictionary is : " + str(test_dict))

res = list(chain(*test_dict.values()))

print("The Concatenated list values are : " + str(res))

Output : 

The original dictionary is : {'Gfg': [4, 5], 'best': [10], 'is': [6, 8]}
The Concatenated list values are : [4, 5, 10, 6, 8]

Method #3 : Using extend(),list() and values() methods

Python3

test_dict = {"Gfg" : [4, 5], "is" : [6, 8], "best" : [10]}

print("The original dictionary is : " + str(test_dict))

x=[]

for i in list(test_dict.values()):

    x.extend(i)

print("The Concatenated list values are : " + str(x))

Output

The original dictionary is : {'Gfg': [4, 5], 'is': [6, 8], 'best': [10]}
The Concatenated list values are : [4, 5, 6, 8, 10]


How do you combine 3 dictionaries?

How to Merge 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. chain().
7) Using dictionary comprehension..
8) Add values of common keys..

How do you combine multiple dictionaries into one in Python?

Python 3.9 has introduced the merge operator (|) in the dict class. Using the merge operator, we can combine dictionaries in a single line of code. We can also merge the dictionaries in-place by using the update operator (|=).

How do I add multiple dictionaries to a list in Python?

Since Python 3.9, it is possible to merge two dictionaries with the | operator. If they have the same key, it is overwritten by the value on the right. You can combine multiple dictionaries. Like += for + , |= for | is also provided.

Is concatenation possible in dictionary Python?

Python concatenate dictionaries with same keys 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)).