carte blanche
carte blanche

Reputation: 11476

Appending values to list of dictionaries

I am trying to append dependent values to a list as below and running into a keyerror...i have the expected output below..can anyone provide inputs on what is wrong here?

'''
Current output without "+=" in internal_dep[num] +=dep_gerrit :-
{'313137': '12345', '99999': '', '34567': '99999', '12345': '34567'}

EXPECTED OUTPUT:-

{'313137': ['12345', '34567': '99999']}

'''
def  findinternaldep(num):
    if num == '313137':
        output ='12345'
    if num == '12345':
        output = '34567'
    if num == '34567':
        output = '99999'
    if num == '99999':
        output = ''
    return output


master_gerrit = ['313137']
internal_dep={}
for num in master_gerrit:
    while True:
        print "NUM"
        print num
        (dep_gerrit)=findinternaldep(num)
        internal_dep[num] +=dep_gerrit
        num = dep_gerrit
        if dep_gerrit == '':
            break
print internal_dep

ERROR:-

Traceback (most recent call last):
  File "test_internal.py", line 34, in <module>
    internal_dep[num] +=dep_gerrit
KeyError: '313137'

Upvotes: 1

Views: 148

Answers (3)

carte blanche
carte blanche

Reputation: 11476

Following is the version that worked for me

list = ['313137','253036']
internal_dep={}
for num in list:
    master_gerrit = num
    while True:
        print "NUM"
        print num
        (dep_gerrit)=findinternaldep(num)
        print "DEP_GERRIT"
        print dep_gerrit
        #print master_gerrit
        print dep_gerrit

        if internal_dep.has_key(master_gerrit):
            internal_dep[master_gerrit].append(dep_gerrit)
        else:
            internal_dep[num]=[dep_gerrit,]
        if dep_gerrit == '':
            break
        num = dep_gerrit
print internal_dep

OUTPUT:- {'313137': ['12345', '34567', '99999', ''], '253036': ['']}

Upvotes: 0

KT100
KT100

Reputation: 1441

You could try the following approach. Basically check if the dict already has the key and if not add a new key else append to the list.

if internal_dep.has_key(num):
    internal_dep[num].append(dep_gerrit)
else:
    internal_dep[num]=[dep_gerrit,]

With this change, your overall code would look like:

mmaster_gerrit = ['313137']
internal_dep={}
for num in master_gerrit:
    while True:
        print "NUM"
        print num
        (dep_gerrit)=findinternaldep(num)
        print "DEP_GERRIT"
        print dep_gerrit
        #print master_gerrit
        #print dep_gerrit
        #print depgerrit_status
        if internal_dep.has_key(num):
            internal_dep[num].append(dep_gerrit)
        else:
            internal_dep[num]=[dep_gerrit,]
        num = dep_gerrit
        if dep_gerrit == '':
            break
print internal_dep

Upvotes: 0

kindall
kindall

Reputation: 184091

You're trying to append to the list before its key exists in the dictionary. The easiest way to address this is to make your internal_dep dictionary a collections.defaultdict. Then, if the key doesn't already exist, a default value (which you can specify) will be created automatically.

import collections
internal_dep = collections.defaultdict(list)

Also note that += is equivalent to list.extend rather than list.append, so you should use one of the following:

internal_dep[num] += [dep_gerrit]   # or
internal_dep[num].append(dep_gerrit)

What you have won't give you an error, but it won't do what you want, either!

Upvotes: 3

Related Questions