2

I have multiple dictionaries with its keys and values and I want to assign(transfer- all of them to a new-empty- dictionary with keeping all keys and values. note: other question that i checked have dictionaries with same size

n = {}
x = {'six':6,'thirteen':13,'fifty five':55}
y = {'two': 2, 'four': 4, 'three': 3, 'one': 1, 'zero': 0,'ten': 10}
z = {'nine': 9, 'four': 4, 'three': 3, 'eleven': 11, 'zero': 0, 'seven':7}
jpp
  • 159,742
  • 34
  • 281
  • 339
Abo
  • 69
  • 1
  • 6

3 Answers3

2

ChainMap

For many use cases, collections.ChainMap suffices and is efficient (assumes Python 3.x):

from collections import ChainMap

n = ChainMap(x, y, z)

n['two']       # 2
n['thirteen']  # 13

If you need a dictionary, just call dict on the ChainMap object:

d = dict(n)

Dictionary unpacking

With Python 3.x, (PEP448), you can unpack your dictionaries as you define a new dictionary:

d = {**x, **y, **z}

Related: How to merge two dictionaries in a single expression?

jpp
  • 159,742
  • 34
  • 281
  • 339
  • 1
    Could also use extended unpacking (which if you've got a Python version that has ChainMap you should have): `n = {**x, **y, **z}`... – Jon Clements Nov 21 '18 at 17:46
  • thank you for helping, but the solutions guys you provided till now will delete duplicate keys and values. However, I want to keep all of them even if dictionaries have duplicated (keys&values) – Abo Nov 21 '18 at 23:37
  • @Abo, A dictionary cannot have duplicate keys. This is a non-negotiable requirement and determined by the structure and use of a dictionary, which maps unique keys to values (although values may be common between multiple keys). – jpp Nov 21 '18 at 23:41
0

Use the dict own update method in a cycle like this:

x = {'six':6,'thirteen':13,'fifty five':55}
y = {'two': 2, 'four': 4, 'three': 3, 'one': 1, 'zero': 0,'ten': 10}
z = {'nine': 9, 'four': 4, 'three': 3, 'eleven': 11, 'zero': 0, 'seven':7}

n = {}
for e in [x,y,z]:
    n.update(e)

It is fast if you have just a few dicts. But if you have several dicts (for example more than 20) better to use the locals().

n = {}
for e in "xyz":
    n.update(locals()[e])

Or if you work with python3 there is an easier way:

n = {**x, **y, **z}
  • Why `locals` here... why not just make it `for e in [x, y, z]:` ? – Jon Clements Nov 21 '18 at 17:52
  • Because on that way you create a list. It uses less memory. –  Nov 21 '18 at 17:55
  • 1
    It's a list of 3 elements that are just holding a reference to the dictionaries... you're calling `locals()` which returns a dictionary of the entire namespace 3 times, then looking up the name, so you've got far more expensive in getting that dictionary 3 times and then performing the lookup... – Jon Clements Nov 21 '18 at 17:56
  • You are right, I have checked it. The list is faster. –  Nov 21 '18 at 18:06
  • Haha, no. With a few dicts you are right. But I see it is faster with locals() if you have several dicts. I got these running times with 20 dicts: With locals: 2.193450927734375e-05 With list: 2.7179718017578125e-05 –  Nov 21 '18 at 18:19
  • thank you for helping, but the solutions guys you provided till now will delete duplicate keys and values. However, I want to keep all of them even if dictionaries have duplicated (keys&values) – Abo Nov 21 '18 at 23:38
0

I read yow want duplicated keys. The dict keys are unique, so in this case convert the data to another data structures, for example into tuples.

your_first_dict = {"a":1, "b":2, "c":3}
your_second_dict = {"a":3, "b":4, "d":5}
list_of_tuples = [(key,your_first_dict[key]) for key in your_first_dict]
list_of_tuples += [(key,your_second_dict[key]) for key in your_second_dict]