24
loading...
This website collects cookies to deliver better user experience
{key:value}
pairs. It is ordered and mutable, and it cannot store duplicate data.my_dict = {
"id": 1,
"name": "Ashutosh",
"books": ["Python", "DSA"]
}
dict_one = {
"id": 1,
"name": "Ashutosh",
"books": ["Python", "DSA"]
}
dict_two = {
"college": "NSEC",
"city": "Kolkata",
"country": "India"
}
merged_dict = {
"id": 1,
"name": "Ashutosh",
"books": ["Python", "DSA"],
"college": "NSEC",
"city": "Kolkata",
"country": "India"
}
merged_dict
we have the key-value pairs of both dict_one
and dict_two
. This is what we wish to achieve programmatically.dict.update()
method**
operator|
(Union) operator (for Python 3.9 and above)>>> dict_one = {
... "id": 1,
... "name": "Ashutosh",
... }
>>> dict_two = {
... "books": ["Python", "DSA"],
... "college": "NSEC",
... }
>>> dict_three = {
... "city": "Kolkata",
... "country": "India"
... }
>>> for key,value in dict_two.items():
... merged_dict[key] = value
...
>>> merged_dict
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC'}
>>> for key,value in dict_three.items():
... merged_dict[key] = value
...
>>> merged_dict
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC', 'city': 'Kolkata', 'country': 'India'}
dict
class, there are various methods inside it. One such method is the update()
method which you can use to merge one dictionary into another.>>> dict_one = {
... "id": 1,
... "name": "Ashutosh",
... "books": ["Python", "DSA"]
... }
>>> dict_two = {
... "college": "NSEC",
... "city": "Kolkata",
... "country": "India"
... }
>>> dict_one.update(dict_two)
>>> dict_one
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC', 'city': 'Kolkata', 'country': 'India'}
update()
method is that it modifies one of the dictionaries. If we wish to create a third dictionary without modifying any of the other dictionaries, we cannot use this method.>>> dict_one = {
... "id": 1,
... "name": "Ashutosh",
... }
>>> dict_two = {
... "books": ["Python", "DSA"],
... "college": "NSEC",
... }
>>> dict_three = {
... "city": "Kolkata",
... "country": "India"
... }
>>> dict_one.update(dict_two)
>>> dict_one
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC'}
>>> dict_one.update(dict_three)
>>> dict_one
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC', 'city': 'Kolkata', 'country': 'India'}
>>> dict_one = {
... "id": 1,
... "name": "Ashutosh",
... }
>>> dict_two = {
... "books": ["Python", "DSA"]
... "college": "NSEC",
... }
>>> dict_three = {
... "city": "Kolkata",
... "country": "India"
... }
>>> merged_dict = { **dict_one,** dict_two, **dict_three}
>>> merged_dict
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC', 'city': 'Kolkata', 'country': 'India'}
**
operator to merge the dictionaries doesn't affect any of the dictionaries. |
) operator to merge two or more dictionaries.>>> dict_one = {
... "id": 1,
... "name": "Ashutosh",
... }
>>> dict_two = {
... "books": ["Python", "DSA"],
... "college": "NSEC",
... }
>>> dict_three = {
... "city": "Kolkata",
... "country": "India"
... }
>>> merged_dict = dict_one | dict_two | dict_three
>>> merged_dict
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC', 'city': 'Kolkata', 'country': 'India'}
|
operator. But if you use older versions of Python, you can still use the other methods discussed above.