-2

List 1

[]

dictionary 1

{ 
   'test': 'baum',
   'alter': 0,
   'voegel': ['amsel']
}

dictionary 2

{ 
   'test': 'grosserbaum',
   'alter': 3,
   'voegel': ['meise']
}

Do something to merge dictionaries together....

Result:

[
   { 
      'test': 'baum',
      'alter': 0,
      'voegel': ['amsel'] 
   }, 
   { 
      'test': 'grosserbaum',
      'alter': 3,
      'voegel': ['meise']
   }
]

How can this be done using Python? Thanks!

FrankTheTank
  • 62
  • 1
  • 11
  • create a list and append two dicts? – mad_ Oct 03 '18 at 15:02
  • 1
    Your requested output throws a `TypeError`. It's not a valid dictionary structure but, rather, an impossible `set()` – roganjosh Oct 03 '18 at 15:02
  • @mad_ Right, I see the data is different. He's looking to make a set of dicts, – Sunny Patel Oct 03 '18 at 15:08
  • To reiterate/add to @roganjosh 's answer, your question is ambiguous. Are you trying to create a nested dictionary of dict objects, or a set of dicts, or are you trying to combine the values from dict1 and dict2 with the same keys, or maybe a JSON file? What object `type()` do you want to have at the end? – G. Anderson Oct 03 '18 at 15:10
  • The result you are showing us is not a dictionary but rather a list of two dictionaries. Keys must be unique and you clearly have repetitions. So you need to think what you want to do in this situation. I would suggest creating a dictionary with the the keys from each dictionary and then for each key in the original dictionaries take the value and insert it into a list. The result will be a dictionary with list values and each list will have the values from both original dictionaries for the same key. – rbaleksandar Oct 03 '18 at 16:45

2 Answers2

1
d1={ 
   'test': 'baum',
   'alter': 0,
   'voegel': ['amsel']
}
d2={ 
   'test': 'grosserbaum',
   'alter': 3,
   'voegel': ['meise']
}

Lets try with your expected output

{d1:d2} #TypeError: unhashable type: 'dict'

The error is raised because the dict type cannot be hashed. similar to the list type as it does not comes with a hash function. Therefore, moral of the story is in order for an object to be hashable, it must be immutable types like string, integer or tuple.

In case you are just looking to iterate over the dicts

[d1,d2] # convert into a list

OR

(d1,d2)# convert into a tuple

or create a new list with immutable keys

{'d1':d1,'d2':d2}

For further reference https://www.asmeurer.com/blog/posts/what-happens-when-you-mess-with-hashing-in-python/

mad_
  • 8,121
  • 2
  • 25
  • 40
0

If the data in your dicts never change, and you want the keys, then you should opt for using namedtuples.

You'll need to convert that nested list into a tuple since lists are not hashable. Then you can take your existing dictionaries and plop them into your namedtuple (or create them directly).

d1 = { 
   'test': 'baum',
   'alter': 0,
   'voegel': ('amsel')
}
d2 = { 
   'test': 'grosserbaum',
   'alter': 3,
   'voegel': ('meise')
}

from collections import namedtuple

Test = namedtuple("Test", "test alter voegel")

t1 = Test(**d1)
t2 = Test(**d2)

print(t1)            # Test(test='baum', alter=0, voegel='amsel')
print(t2)            # Test(test='grosserbaum', alter=3, voegel='meise')
print(set([t1, t2])) # {Test(test='grosserbaum', alter=3, voegel='meise'), Test(test='baum', alter=0, voegel='amsel')}

Now you can keep those namedtuples inside a set so that collection will contain only unique items! Also, namedtuples use less memory than a dict since it implements __slots__.

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46