Kalariya_M
Kalariya_M

Reputation: 1403

addition of more than one dict in python

i have 6 dict like this

dict1
dict2
dict3
dict4
dict5
dict6

now I want all of this in one dict. so I used this

dict1.update({'dict2':dict2}) 
dict3.update({'dict1':dict1})
dict4.update({'dict4':dict3})
dict5.update({'dict5':dict4})                           
dict6.update({'dict6':dict5})

at last dict6 contains all value but it's not formatted correctly and it's not the pythonic way to do it

I want to improve this any suggestions

right now I'm getting like this but I don't want like this

{"main_responses": {"dict1": {"dict2": {"dict3": {"dict4": {"dict5": {}}}}}}}

i want

{"main_responses":{ "dict1": {dict1_values}, "dict2": {dict2_values}..... and so on

Upvotes: 0

Views: 96

Answers (6)

Kaushik NP
Kaushik NP

Reputation: 6789

Giving a very similar example of my own based on your requirements:

>>> d1 = {'a':1}
>>> d2 = {'b':2}
>>> d3 = {'c':3}
>>> d4 = {'d':4}

#magic happens here
>>> d = {'d1':d1 , 'd2':d2, 'd3':d3, 'd4':d4}
>>> d
=> {'d1': {'a': 1}, 'd2': {'b': 2}, 'd3': {'c': 3}, 'd4': {'d': 4}}

Since you do not have all the dictionaries that you want added in one place, this is about as easy as it gets.

In case you want to add another key to your collection of all dictionaries (d here), do:

>>> out = {'api_responses': d}

#or in one step if you do not want to use `d`
>>> out = {'api_responses': {'d1':d1 , 'd2':d2, 'd3':d3, 'd4':d4}}

>>> out
=> {'api_responses': {'d1': {'a': 1}, 'd2': {'b': 2}, 'd3': {'c': 3}, 'd4': {'d': 4}}}

Upvotes: 1

Mohamed Ali JAMAOUI
Mohamed Ali JAMAOUI

Reputation: 14699

Try this:

from itertools import chain
d = chain.from_iterable(d.items() for d in (ada_dict, 
                                        wordpress_version_dict,
                                        drupal_version_dict,
                                        ssl_dict,
                                        link_dict,
                                        tag_dict)) 

api_response = {'api_response':d}

Or this, using reduce:

d = reduce(lambda x,y: dict(x, **y), (ada_dict, 
                                  wordpress_version_dict,
                                  drupal_version_dict,
                                  ssl_dict,
                                  link_dict,
                                  tag_dict))

api_response = {'api_response':d}

Upvotes: 2

Argus Malware
Argus Malware

Reputation: 787

 a =[ada_dict,
wordpress_version_dict,
drupal_version_dict,
ssl_dict,
link_dict,
tag_dict]

 c= {}

for i in a:
    c.update({i:i})

print c
{'ada_dict': 'ada_dict',
 'drupal_version_dict': 'drupal_version_dict',
 'link_dict': 'link_dict',
 'ssl_dict': 'ssl_dict',
 'tag_dict': 'tag_dict',
 'wordpress_version_dic': 'wordpress_version_dic'}
d={}
d.update({"api_responses":c})
print d

{'api_responses': {'ada_dict': 'ada_dict',
  'drupal_version_dict': 'drupal_version_dict',
  'link_dict': 'link_dict',
  'ssl_dict': 'ssl_dict',
  'tag_dict': 'tag_dict',
  'wordpress_version_dic': 'wordpress_version_dic'}}

Upvotes: 0

Indent
Indent

Reputation: 4967

If you want add all dict in a single one "newDict", Be carrefull if several keys exist in multiple Dict :

ada_dict={'k1':'v1'}
wordpress_version_dict={'k2':'v2'}
drupal_version_dict={'k3':'v3'}
ssl_dict={'k4':'v4'}
link_dict={'k5':'v5'}
tag_dict={'k5':'v5'}

newDict={}
newDict.update( (k,v) for k,v in ada_dict.iteritems() if v is not None)
newDict.update( (k,v) for k,v in wordpress_version_dict.iteritems() if v is not None)
newDict.update( (k,v) for k,v in drupal_version_dict.iteritems() if v is not None)
newDict.update( (k,v) for k,v in ssl_dict.iteritems() if v is not None)
newDict.update( (k,v) for k,v in link_dict.iteritems() if v is not None)
newDict.update( (k,v) for k,v in tag_dict.iteritems() if v is not None)

print {'api_response':newDict}

https://repl.it/ND3p/1

Upvotes: 1

Humi
Humi

Reputation: 609

Does this help in getting the output you want?

   new_dict=dict(ada_dict.items()+wordpress_version_dict.items() +drupal_version_dict.items()+ssl_dict.items()+link_dict.items()+tag_dict.items())

Upvotes: 0

pd shah
pd shah

Reputation: 1406

please add more comment to your post, but as I see you need something like , is not it ?

>>> foo=lambda dest, src, tag: [dest.update({tag:i}) for i in src]
>>> x={1:1}
>>> y={2:2}
>>> foo(x, y, "tag")
[None]
>>> x
{1: 1, 'tag': 2}
>>> y
{2: 2}

Upvotes: 0

Related Questions