Linked Questions

369 votes
5 answers
641k views

How to concatenate two dictionaries to create a new one? [duplicate]

Say I have three dicts d1={1:2,3:4} d2={5:6,7:9} d3={10:8,13:22} How do I create a new d4 that combines these three dictionaries? i.e.: d4={1:2,3:4,5:6,7:9,10:8,13:22}
timy's user avatar
  • 3,783
234 votes
4 answers
154k views

Union of dict objects in Python [duplicate]

How do you calculate the union of two dict objects in Python, where a (key, value) pair is present in the result iff key is in either dict (unless there are duplicates)? For example, the union of {'a'...
Mechanical snail's user avatar
50 votes
4 answers
114k views

TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items' [duplicate]

I try to sum two dictionaries like that: my_new_dict = dict(my_existing_dict.items() + my_new_dict.items()) but recieve error TypeError: unsupported operand type(s) for +: 'dict_items' and '...
JohnDow's user avatar
  • 1,322
51 votes
2 answers
7k views

How to merge multiple json objects into a single json object using python [duplicate]

I have a list which contains multiple JSON objects and I would like to combine those JSON objects into a single json object, I tried using jsonmerge but with no luck. My list is: t = [{'ComSMS': '...
user avatar
19 votes
3 answers
11k views

Combine two dictionaries with preference to one of them - [duplicate]

I have two dictionaries One: default = {"val1": 10, "val2": 20, "val3": 30, "val4": 40} Two: parsed = {"val1": 60, "val2": 50} Now, I want ...
Rohan Asokan's user avatar
20 votes
5 answers
4k views

Merging of two dictionaries [duplicate]

I have two dictionaries as follows: D1={'a':1,'b':2,'c':3} and D2={'b':2,'c':3,'d':1} I want to merge these two dictionaries and the result should be as follows: D3={'a':1,'b':2,'c':3,'b':2,'c':...
Pravitha V's user avatar
  • 3,318
30 votes
3 answers
6k views

How to construct a dictionary from two dictionaries in python? [duplicate]

Let's say I have a dictionary: x = {"x1":1,"x2":2,"x3":3} and I have another dictionary: y = {"y1":1,"y2":2,"y3":3} Is there any neat way to constract a 3rd dictionary from the previous two: z = {...
Mero's user avatar
  • 1,330
28 votes
1 answer
4k views

Union of dictionaries python [duplicate]

I have two dictionaries that I want the union of so that each value from the first dictionary is kept and all the key:value pairs from the second dictionary is added to the new dictionary, without ...
NicolaiF's user avatar
  • 1,323
16 votes
3 answers
779 views

How do I get union keys of `a` and `b` dictionary and 'a' values? [duplicate]

I was doing django project about processing request.data and from.cleaned_data issues. When user only inputs specify fields, it will send request to my server. Next, the form class process the request,...
Burger King's user avatar
  • 2,955
7 votes
3 answers
1k views

How can I concatenate dicts (values to values of the same key and new key)? [duplicate]

I have a problem with concatenating dictionaries. Have so much code so I show in example what my problem is. d1 = {'the':3, 'fine':4, 'word':2} + d2 = {'the':2, 'fine':4, 'word':1, 'knight':1, '...
MTG's user avatar
  • 301
20 votes
1 answer
2k views

python: merging dictionaries by identical value of key [duplicate]

I am new to python and have looked at other answers on merging dictionaries but was still a bit confused. I am looking to merge together two dictionaries in python by a common value within a specific ...
RCN's user avatar
  • 671
2 votes
2 answers
4k views

Merge a dict in Python using 1 dict as base [duplicate]

I am looking for feedback on my Python code. I am trying to merge two dictionaries. One of the dictionaries controls the structure and the default values, the second dictionary will overwrite the ...
Patrick's user avatar
  • 303
2 votes
1 answer
4k views

Python CodeLab dictionary-traversal [duplicate]

The question is This is what I have so far: dict(nafta_capitals) = canadian_capitals, mexican_capitals, us_capitals Given three dictionaries, associated with the variables , canadian_capitals, ...
Anthony Tesoriero's user avatar
1 vote
0 answers
2k views

Python: update a nested dictionary [duplicate]

Let's say I am given a nested dicionary my_dict and another nested dict update: a sequence of keys (valid in my_dict) with a value to be assigned to the leaf at this sequence. For example, my_dict = { ...
SBF's user avatar
  • 355
2 votes
2 answers
604 views

Adding a Key-Value pair in a list in an empty dictionary if the keys are the same [duplicate]

How can I print a dictionary as a union of given dictionaries & appending the values if the keys are equal? Input: dict_1 = { "x":[1,"hello"], "y":[2,"...
ChocoApplePiez's user avatar

15 30 50 per page
1
2 3 4 5
25