i am having two dictionaries
first = {'id': 1, 'age': 23} second = {'id': 4, 'out': 100}
I want output dictionary as
{'id': 5, 'age': 23, 'out':100}
I tried
>>> dict(first.items() + second.items()) {'age': 23, 'id': 4, 'out': 100}
but i am getting id as 4 but i want to it to be 5 .
You can simply update the
'id'
key afterwards:If you want to add values from the second to the first, you can do it like this:
The above will output:
You want to use collections.Counter:
Output:
And if you need the result as a true
dict
, just usedict(first_plus_second)
: