IAMbeginner
IAMbeginner

Reputation: 269

Python: How to join dictionaries in for loop

is there any way to join dictionaries as a result in for loop. Here is my sample code:

for value in data['actions']:
    if 'remoteUrls' in value:
        url = value['remoteUrls']
        ref = value['lastBuiltRevision']['SHA1']
        new_dict['url'] = url
        new_dict['ref'] = ref
        print new_dict

results:

{
    'url': [u'ssh://abc.com:29418/abc.git'],
    'ref': u'194d4c418c71f77355117bd253cf2ac9849b25dd'
}
{
    'url': [u'ssh://def:29418/def.git'],
    'ref': u'7a198bf01b73330c379cc54aae1631f4448a4b0b'
}

I want to join the results into one dictionary and the desired output is like this:

{
    vcs1: {
        'url': [u'ssh://abc.com:29418/abc.git'],
        'ref': u'194d4c418c71f77355117bd253cf2ac9849b25dd'
    },
    vcs2: {
        'url': [u'ssh://def:29418/def.git'],
        'ref': u'7a198bf01b73330c379cc54aae1631f4448a4b0b'
    }
}

Is there any way to achieve the desired output? Any help would be appreciated. Thank you.

Upvotes: 0

Views: 3203

Answers (2)

Austin
Austin

Reputation: 26039

This is one way:

lst = [{'url': [u'ssh://abc.com:29418/abc.git'],'ref':u'194d4c418c71f77355117bd253cf2ac9849b25dd'}, 
       {'url': [u'ssh://def:29418/def.git'], 'ref': u'7a198bf01b73330c379cc54aae1631f4448a4b0b'}]

i = (i for i in range(len(lst)))
d = {'vcs{}'.format(next(i) + 1): x for x in lst}

print(d)
# {'vcs1': {'url': ['ssh://abc.com:29418/abc.git'], 'ref': '194d4c418c71f77355117bd253cf2ac9849b25dd'}, 
#  'vcs2': {'url': ['ssh://def:29418/def.git'], 'ref': '7a198bf01b73330c379cc54aae1631f4448a4b0b'}}                     

Or using itertools.count as suggested in comments:

from itertools import count

lst = [{'url':[u'ssh://abc.com:29418/abc.git'],'ref':u'194d4c418c71f77355117bd253cf2ac9849b25dd'}, 
       {'url': [u'ssh://def:29418/def.git'], 'ref': u'7a198bf01b73330c379cc54aae1631f4448a4b0b'}]

i = count(1)
d = {'vcs{}'.format(next(i)): x for x in lst}

print(d)
# {'vcs1': {'url': ['ssh://abc.com:29418/abc.git'], 'ref': '194d4c418c71f77355117bd253cf2ac9849b25dd'}, 
#  'vcs2': {'url': ['ssh://def:29418/def.git'], 'ref': '7a198bf01b73330c379cc54aae1631f4448a4b0b'}}

Or this is even simple using enumerate:

d = {'vcs{}'.format(i): x for i, x in enumerate(lst, 1)}

Upvotes: 2

Sunil Kumar
Sunil Kumar

Reputation: 226

There are some easy methods,

  • dict.update() will help you join dictionaries.
>>> a = dict()
>>> a.update({1:2})
>>> a
{1: 2}
>>> a.update({3:4})
>>> a
{1: 2, 3: 4}
  • dict['key'] = {'url':['URL'], 'ref':'REF'}
>>> a['key123'] = {'url':['url1','url2'], 'ref':'REF'}
>>> a
{1: 2, 'key1': {'a': 'hello'}, 3: 4, 'key123': {'url': ['url1', 'url2'], 'ref': 'REF'}, 'key2': {'url': ['URL1', 'URL2'], 'ref': u'ref'}}

As per your case,

res = dict()
for value in data['actions']:
    if 'remoteUrls' in value:
        res['key_name'] = {'url':value['remoteUrls'] ,
                           'ref':value['lastBuiltRevision']['SHA1']}
print res # check the entries
  • dict comprehension

dict(dict(<key>, {'url':value['remoteUrls'], 'ref':value['lastBuiltRevision']['SHA1']} for value in data['actions'] if 'remoteUrls' in value)

Upvotes: 1

Related Questions