-2

can a dictionary add to a dictionary?

I have two dictionaries with the same key.

A = {"function":"try","args":tuple(),"size":1}
B = {"function":"test","args":tuple(),"size":2}

I don't want to struct the dict as this one

C = {key1 = {"function":"try","args":tuple(),"size":1}, key2= {"function":"test","args":tuple(),"size":2}}

since I want to combine them to the same dictionary as this

C = {{"function":"try","args":tuple(),"size":1},{"function":"test","args":tuple(),"size":2}}

How can I remove the key1 and key2, it is possible to do that? many thanks.

jordanm
  • 33,009
  • 7
  • 61
  • 76
Yan
  • 7
  • 7
  • 2
    dictionaries require keys. what you have there as what you want looks more like a list or set of dictionaries. if you want to combine the two dictionaries you can do that with e.g. `A.update(B)` – dan_g Jun 19 '19 at 16:06
  • 4
    Your desired result isn't a dictionary, it doesn't have any keys. – Barmar Jun 19 '19 at 16:06
  • 1
    Your `C` example isn't a dictionary, it's a set containing two dictionaries. – John Gordon Jun 19 '19 at 16:07
  • 1
    Yes you can have a dictionary of dictionaries, no they need to have keys. Do you want a list? – Mitch Jun 19 '19 at 16:07
  • @JohnGordon It's an *attempt* at a set; a `dict` isn't hashable, so you can't have a set of `dicts. – chepner Jun 19 '19 at 16:07
  • 5
    Just make a list: `C = [A, B]` – Barmar Jun 19 '19 at 16:08
  • I don't want to create a list – Yan Jun 19 '19 at 16:11
  • @Yan Why is a list not suitable for you? – Dan Jun 19 '19 at 16:13
  • if you use .update the elemets of the dict B will be placed in the dict A and when u use list the type of the dict C will be a list C will be a dict of lists – Ghassen Jun 19 '19 at 16:15
  • May be people can suggest an alternative approach for your requirement if you explain more on what needs to be achieved. Internally keys are stored as hashes(non repeatable) and their corresponding values get updated when you try to merge. Please try executing the statement `{True: 'yes', 1: 'no', 1.0: 'May be'}` to check the output. – Jitendra Jun 19 '19 at 16:29
  • Since I want to create a configuration file and the format need to be C = {{"function":"try","args":tuple(),"size":1},{"function":"test","args":tuple(),"size":2}} – Yan Jun 19 '19 at 16:49
  • 1
    @Yan you're gonna have to be more open-minded than that, what you desire isn't a data structure that Python supports. What is the larger concept you're trying to achieve with this data structure? Nobody wants a particular data structure for its own sake. – Tom Lubenow Jun 19 '19 at 16:54
  • so is there any way to create a set of the dictionary? @TomLubenow – Yan Jun 19 '19 at 16:56
  • You could theoretically create a custom dictionary class which supports being hashed, and then you could create a set of those. However, any hash function you would create for dictionaries would not be O(1), meaning any advantage to keeping it in a set instead of a list would be lost. The better question is, why do you want a set of dictionaries? Do you think doing so will allow you to achieve O(1) tests for membership? – Tom Lubenow Jun 19 '19 at 17:03

2 Answers2

1

From your question, it is not possible to create a dict of dicts in the fashion you described:

{
  {
    "function": "try",
    "args":tuple(),
    "size":1
  },
  {
    "function": "test",
    "args":tuple(),
    "size":2
  }
}

BUT you can structure it as a list of dicts:

[
  {
    "function": "try",
    "args": tuple(),
    "size": 1
  },
  {
    "function": "test",
    "args": tuple(),
    "size": 2
  }
]

The reason being that a dict is actually an associative array (two arrays whose indices line up with each other), so it MUST be in the format:

{
  'key1': {
     "function": "try",
     "args": tuple(),
     "size":1
   },
   'key2': {
     "function": "test",
     "args": tuple(),
     "size": 2
   }
}

FelizNaveedad
  • 358
  • 2
  • 7
0

You could update a blank dict with the keys of the other dicts.

c = {}
c.update(dict(a[key1]))
c.update(dict(b[key2]))
Ryan Stefan
  • 124
  • 1
  • 3