6

I have several dictionaries like this

dict1 = {0: 33.422, 1: 39.2308, 2: 30.132}
dict2 = {0: 42.2422, 1: 43.342, 2: 42.424}
dict3 = {0: 13.422, 1: 9.2308, 2: 20.132}

I am aware that I could combine them together into one dictionary using the code

dicts = dict1, dict2, dict3

And it returns the result like this

({0: 33.422, 1: 39.2308, 2: 30.132}, {0: 42.2422, 1: 43.342, 2: 42.424}, {0: 13.422, 1: 9.2308, 2: 20.132})

However, what if the dictionaries come sequentially? How can I get the same results? I tried the following code

dicts = {}
dicts = dicts, dict1
dicts = dicts, dict2
dicts = dicts, dict3

But it returns the result like this

((({}, {0: 33.422, 1: 39.2308, 2: 30.132}), {0: 42.2422, 1: 43.342, 2: 42.424}), {0: 13.422, 1: 9.2308, 2: 20.132})

How can we remove the first stuff? I'm using python 3 on windows 7. And all the dictionaries are in a "MyDataFileReader" type under avro package.

ycenycute
  • 688
  • 4
  • 10
  • 20
  • 1
    `dicts = dict1, dict2, dict3` will create a tuple and not a dictionary ... So what you are doing is just making tuples of tuples – MMF Nov 26 '16 at 15:53
  • When using the 'double assignment' `dict = dicts, dict1`, essentially you are creating a `tuple`. Tuples are immutable and therefore you cant add anything to it with the following lines (`dicts = dicts, dict2` etc.) it simply creates a new tuple containing the old tuple. I'm not sure what you want as a final datatype. Could you elaborate on that? – Eljee Nov 26 '16 at 15:55
  • What do you need the data to look like in the end? Are you just looking to combine multiple dictionaries? – Dimitris Fasarakis Hilliard Nov 26 '16 at 15:56
  • You can't append to a dictionary. `dict` keys are unique so you can't just add another one to the end. In your example, each dict has a `0` key. If you combine the dicts, what do you want to happen to these keys (what should happen to `0: 33.422` `0: 42.2422` and `0: 13.422`)? – tdelaney Nov 26 '16 at 16:02
  • [ChainMap](http://stackoverflow.com/q/23392976/2096752) can be useful in cases like this. – shx2 Nov 26 '16 at 16:04
  • @Tracy Yang: what result are you looking for? would be nice to know that. Also, check the first solution given below, depends on the solution desired, you have some of them. – wj127 Nov 26 '16 at 16:18

3 Answers3

9

If I understand correctly you want a list of dictionaries:

dict1 = {0: 33.422, 1: 39.2308, 2: 30.132}
dict2 = {0: 42.2422, 1: 43.342, 2: 42.424}
dict3 = {0: 13.422, 1: 9.2308, 2: 20.132}
dicts = []
dicts.append(dict1)
dicts.append(dict2)
dicts.append(dict3)
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
6

As @MMF already stated in his comment: a, b, c creates a tuple containing a, b and c (in that order).

What you want to do, is "updating" the dictionary:

dict = {}
dict.update(dict1)
dict.update(dict2)
dict.update(dict3)

Or, if you don't want that dict3 or dict2 overwrite stuff:

dict = {}
dict.update(dict3)
dict.update(dict2)
dict.update(dict1)

Or

def update_without_overwriting(d, x):
    dict.update({k: v for k, v in x.items() if k not in d})

dict = {}
update_without_overwriting(dict, dict1)
update_without_overwriting(dict, dict2)
update_without_overwriting(dict, dict3)

If you just want a tuple containing all dicts however, use this:

dict = dict1, dict2
dict += dict3,
CodenameLambda
  • 1,486
  • 11
  • 23
-1

From the first part of your examples, it appears you're trying to get a tuple of dictionaries, and not a dict of dicts. So try this:

dicts = ()
dicts = dicts + (dict1,)
dicts = dicts + (dict2,)
dicts = dicts + (dict3,)

Note that the trailing comma is required...

Thejaka Maldeniya
  • 1,076
  • 1
  • 10
  • 20