0

I have code as mentioned below.

a={'test': { 'test':'Test2','scale':'scale4'},
   'test2': {'test':'test5','scale':'scale44'},
   'test3': { 'test':'Test2','scale':'scale4'}}

dict1={}
blue_print={}

for i,v in a.items():
   blue_print['scale']=a[i].get('scale')
   blue_print['test'] =a[i].get('test')
dict1.update(blue_print)
print(dict1)

The above showing the output only:

{'scale': 'scale4', 'test': 'Test2'}

I want the output should displayed like below:

{{'scale': 'scale4', 'test': 'Test2'},
 {'scale': 'scale44', 'test': 'test5'},
 {'scale': 'scale4', 'test': 'Test2'}}
martineau
  • 119,623
  • 25
  • 170
  • 301
veeramani
  • 11
  • 2
  • Does this answer your question? [How do I merge two dictionaries in a single expression (taking union of dictionaries)?](https://stackoverflow.com/questions/38987/how-do-i-merge-two-dictionaries-in-a-single-expression-taking-union-of-dictiona) – ᴓᴓᴓ Oct 05 '21 at 16:39
  • 5
    The expected output you showed isn't a valid dictionary, though. Dictionaries _cannot_ have duplicate keys, so each line like `blue_print['scale']=a[i].get('scale')` in a loop _overwrites_ the previous values of `blue_print['scale']`, so when you do `dict1.update(blue_print)`, `blue_print` will contain data from the _last_ iteration only – ForceBru Oct 05 '21 at 16:41
  • 1
    Dictionaries also cannot be placed into sets. Did you want to use a list? – OneCricketeer Oct 05 '21 at 16:46
  • 1
    The provided "dictionary" at the end doesn't actually have any keys, it's just a list of objects. – h0r53 Oct 05 '21 at 16:46

2 Answers2

0

If I understand correctly, you want a list of the values in the input dict, not merging anything (which you don't want because each test/scale key would get overriden, as you've found out)

>>> a={'test': { 'test':'Test2','scale':'scale4'},'test2':{'test':'test5','scale':'scale44'},'test3': { 'test':'Test2','scale':'scale4'}}
>>> [v for _, v in a.items()]
[{'test': 'Test2', 'scale': 'scale4'}, {'test': 'test5', 'scale': 'scale44'}, {'test': 'Test2', 'scale': 'scale4'}]

Or list(a.values())

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0
dict1 = list(a.values())
>>> print(dict1)
[{'test': 'Test2', 'scale': 'scale4'}, {'test': 'test5', 'scale': 'scale44'}, {'test': 'Test2', 'scale': 'scale4'}]
Was'
  • 496
  • 4
  • 21