0

I need to update a single nested dictionary at once.

I have a for loop where rows is a NodeList (minidom)

Where dictKeys looks like this :

dictKeys = {'LastEditDate': {}, 'LastEditorUserId': {}, 'LastActivityDate': {}, 'Id': {}, 'CommentCount': {}, 'ParentId': {}, 'CreationDate': {}, 'Title': {}, 'PostTypeId': {}, 'AnswerCount': {}, 'ClosedDate': {}, 'OwnerUserId': {}, 'ViewCount': {}, 'Body': {}, 'Score': {}, 'AcceptedAnswerId': {}, 'OwnerDisplayName': {}, 'Tags': {}, 'FavoriteCount': {}}

for index,element in enumerate(rows):
    for key in dictKeys.keys():
        dictKeys[key][index] = ""
    for attrName, attrValue in element.attributes.items():
        dictKeys[attrName][index]= attrValue

Instead of each key value pair updating on each iteration, they're all updating at once.

{'LastEditDate': {0: '11062'}, 'LastEditorUserId': {0: '11062'}, 'LastActivityDate': {0: '11062'}, 'Id': {0: '11062'}, 'CommentCount': {0: '11062'}, 'ParentId': {0: '11062'}, 'CreationDate': {0: '11062'}, 'Title': {0: '11062'}, 'PostTypeId': {0: '11062'}, 'AnswerCount': {0: '11062'}, 'ClosedDate': {0: '11062'}, 'OwnerUserId': {0: '11062'}, 'ViewCount': {0: '11062'}, 'Body': {0: '11062'}, 'Score': {0: '11062'}, 'AcceptedAnswerId': {0: '11062'}, 'OwnerDisplayName': {0: '11062'}, 'Tags': {0: '11062'}, 'FavoriteCount': {0: '11062'}}

and

{'LastEditDate': {0: '2'}, 'LastEditorUserId': {0: '2'}, 'LastActivityDate': {0: '2'}, 'Id': {0: '2'}, 'CommentCount': {0: '2'}, 'ParentId': {0: '2'}, 'CreationDate': {0: '2'}, 'Title': {0: '2'}, 'PostTypeId': {0: '2'}, 'AnswerCount': {0: '2'}, 'ClosedDate': {0: '2'}, 'OwnerUserId': {0: '2'}, 'ViewCount': {0: '2'}, 'Body': {0: '2'}, 'Score': {0: '2'}, 'AcceptedAnswerId': {0: '2'}, 'OwnerDisplayName': {0: '2'}, 'Tags': {0: '2'}, 'FavoriteCount': {0: '2'}}

I am wondering how to do this properly?

Arik
  • 205
  • 3
  • 8
  • can you add the value of rows and also your expected output? – Rieljun Liguid Mar 28 '20 at 04:28
  • This is not really a duplicate, and I'm not sure I understand your question, but see if https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel helps. – Karl Knechtel Mar 28 '20 at 04:29

1 Answers1

0

If you have an empty dictionary

d = {}

and use it as an initial item value

dictKeys = {'a': d, 'b': d, 'c': d}

then a single dictionary d still exists, referenced from all places.

Even though you update a single item

dictKeys['a'][0] = 'hello'

this update seemingly occurs everywhere.

assert dictKeys == {'a': {0: 'hello'}, 'b': {0: 'hello'}, 'c': {0: 'hello'}}
dlask
  • 8,776
  • 1
  • 26
  • 30
  • I understand, but now I am wondering why would dictKeys = {'a': {}, 'b': {}, 'c': {}} use the same dictionary referenced from all places? Would I really have to create 18 different variables? There has to be a better way to initialize a new dictionary. – Arik Mar 28 '20 at 19:37
  • I cannot reproduce this behaviour in Python 3.7.5. Do you use an older version? – dlask Mar 28 '20 at 20:16
  • the function I was using to create my dictionary was initializing from a single instance. That's why I was producing the behavior. – Arik Mar 28 '20 at 20:49