In this simple python program, we need to concatenate two dictionaries. It is a number-based python program.
For a better understanding of this example, we always recommend you to learn the basic topics of Python programming listed below:
In this simple python program, we need to concatenate two dictionaries. A Dictionary is a collection of values that is unordered and has a key and value pair. We use the dictionary to retrieve the value when we know the key easily. We can add the elements into a python dictionary using the braces and put the elements in the braces with comma.
Every element in the dictionary has a key and a value that is associated with the key represented as a key-value pair. Values may be of anything or any type, which may be a repeat or not. But for a key, it must not be a repeat, and it must be a tuple, number, or string.
In this python program, we need to concatenate the dictionaries, which means we have to join the two dictionaries and make a single dictionary. For joining the dictionaries, we are using two pre-defined dictionaries with some values in them. Then we are using the update
method in python to update the di and d2 dictionaries together and make them concatenated. Finally, we are printing the concatenated dictionary using print in python language.
The update ()
method in dictionaries in python is used to update a dictionary with the other dictionary object or a key-value pair. Syntax of the update method is Syntax: dict.update([other])
STEP 1: Use two dictionaries with some predefined key-value pairs in them.
STEP 2: Use the update
method to concatenate two dictionaries together.
STEP 3: Print the concatenated dictionary using the print
function in python.
d1={'A':1,'B':2}
d2={'C':3}
d1.update(d2)
print("dictionary concatenated is:")
print(d1)
dictionary concatenated is: {'A': 1, 'B': 2, 'C': 3}