Reputation: 13
I am working with a json file that I have now converted to a nested dictionary with the following structure:
dictionary = {
'4123bf9f': {'date': '', 'link': '', 'topic': ''},
'45361a6c': {'date': '', 'link': '', 'topic': ''},
'4d521c71': {'date': '', 'link': '', 'topic': ''},
'5b00c155': {'date': '', 'link': '', 'topic': ''},
'875737f4': {'date': '', 'link': '', 'topic': ''},
'90a64c61': {'date': '', 'link': '', 'topic': ''}
} etc
I am trying to loop through the nested dictionary and change the values for the keys, using items from a list.
For example:
topics = ['1', '2', '3', '4', '5 ', '6']
When integrated would become:
dictionary = {
'4123bf9f': {'date': '', 'link': '', 'topic': '1'},
'45361a6c': {'date': '', 'link': '', 'topic': '2'},
'4d521c71': {'date': '', 'link': '', 'topic': '3'},
'5b00c155': {'date': '', 'link': '', 'topic': '4'},
'875737f4': {'date': '', 'link': '', 'topic': '5'},
'90a64c61': {'date': '', 'link': '', 'topic': '6'}
} etc
I have tried a few different approaches but each has resulted in the same output. I realised the code below is probably too simplistic an approach but for sake of clarity of my problem, the following code:
def update_dic(dictionary):
for k, v in dictionary.items():
dictionary[k].update({"topic": n for n in topics})
Results in this output
dictionary = {
'4123bf9f': {'date': '', 'link': '', 'topic': '6'},
'45361a6c': {'date': '', 'link': '', 'topic': '6'},
'4d521c71': {'date': '', 'link': '', 'topic': '6'},
'5b00c155': {'date': '', 'link': '', 'topic': '6'},
'875737f4': {'date': '', 'link': '', 'topic': '6'},
'90a64c61': {'date': '', 'link': '', 'topic': '6'}
} etc
Is it possible to loop through my nested dictionary, replacing "topic" with a distinct value from my list? I have considered trying to make each page in my dictionary a class but was unsure if this was an appropriate method
Any help with this would be greatly appreciated
Upvotes: 0
Views: 566
Reputation: 4233
dictionary = {
'4123bf9f': {'date': '', 'link': '', 'topic': ''},
'45361a6c': {'date': '', 'link': '', 'topic': ''},
'4d521c71': {'date': '', 'link': '', 'topic': ''},
'5b00c155': {'date': '', 'link': '', 'topic': ''},
'875737f4': {'date': '', 'link': '', 'topic': ''},
'90a64c61': {'date': '', 'link': '', 'topic': ''}
}
topics = ['1', '2', '3', '4', '5 ', '6']
topic=iter(topics)
for key in dictionary.keys():
dictionary[key]['topic']=next(topic)
print(dictionary)
output:
{'4123bf9f': {'date': '', 'link': '', 'topic': '1'}, '45361a6c': {'date': '', 'link': '', 'topic': '2'}, '4d521c71': {'date': '', 'link': '', 'topic': '3'}, '5b00c155': {'date': '', 'link': '', 'topic': '4'}, '875737f4': {'date': '', 'link': '', 'topic': '5 '}, '90a64c61': {'date': '', 'link': '', 'topic': '6'}}
Upvotes: 0
Reputation: 27588
Just another way:
topic = iter(topics)
for d in dictionary.values():
d['topic'] = next(topic)
And another:
for d, d['topic'] in zip(dictionary.values(), topics):
pass
Upvotes: 1
Reputation: 780798
Your dictionary comprehension is creating dictionary elements for every item in the list each time. But since you give them all the same key topic
, only the last one is in the resulting dictionary. That's why you keep getting "topic": 6
.
Use zip()
to iterate through the dictionary keys and list elements in parallel, rather than nesting them. And there's no need to use update()
if you're just setting one key of the dictionary, just assign to that key.
def update_dic(dictionary):
for k, v in zip(dictionary, topics):
dictionary[k]['topic'] = v
Upvotes: 2