There is a type dictionary

z = { 'a': 1, 's': [{'d': 2, 'f': 3}{'d': 2, 'f': 3}{'d': 2, 'f': 3}]} 

it is necessary to add a key \ value from the main dictionary to each of the nested ones without changing the structure

 z = { 'a': 1, 's': [{'d': 2, 'f': 3,'a': 1}{'d': 2, 'f': 3,'a': 1}{'d': 2, 'f': 3,'a': 1}]} 
  • What is your problem? - Sergey Gornostaev
  • for i in z ['a']: z ['a'] [i] .append (z ['a']) there is a problem with the data type - djt111
  • one
    for d in z['s']: d['a'] = z['a'] - Sergey Gornostaev

1 answer 1

Let's write a small function for convenience:

 def update_dicts(dicts, **kwds): for d in dicts: d.update(**kwds) 

Further we will call it on the presented data:

 z = { 'a': 1, 's': [{'d': 2, 'f': 3}, {'d': 2, 'f': 3}, {'d': 2, 'f': 3}]} update_dicts(z['s'], a=z['a'])