Reputation: 982
So i'm practicing my nested dictionary skills with this data:
content = {'results':
[
{'_class': 'question',
'course': {'_class': 'course',
'title': 'Angular 7 (formerly Angular 2) - The Complete Guide',
'url': '/the-complete-guide-to-angular-2/'},
'replies': [{'_class': 'answer',
'user': {'_class': 'user',
'display_name': 'Maximilian'}}]},
{'_class': 'question',
'course': {'_class': 'course',
'title': 'RUSTCRACEON',
'url': '/the-holy-rust-book/'},
'replies': [{'_class': 'answer',
'user': {'_class': 'user',
'display_name': 'Ella'}}]},
{'_class': 'question',
'course': {'_class': 'course',
'title': 'PHP ULTIMATE WEB GUIDE',
'url': '/php-to-the-world/'},
'replies': [{'_class': 'answer',
'user': {'_class': 'user',
'display_name': 'Rick_Sanchez'}}]},
{'_class': 'question',
'course': {'_class': 'course',
'title': 'Javascript - The Complete Guide',
'url': '/javascript291/'},
'replies': [{'_class': 'answer',
'user': {'_class': 'user',
'display_name': 'Morty'}}]}
]}
I'm able to access the value in the results
key using this and all is good:
question_access = content['results']
If I want to loop through the dictionaries inside the results
key I use this:
for question_data in question_access:
print(question_data)
Which gives me this.. all is good:
{'_class': 'question', 'course': {'_class': 'course', 'title': 'Angular 7 (formerly Angular 2) - The Complete Guide', 'url': '/the-complete-guide-to-angular-2/'}, 'replies': [{'_class': 'answer', 'user': {'_class': 'user', 'display_name': 'Maximilian'}}]}
{'_class': 'question', 'course': {'_class': 'course', 'title': 'RUSTCRACEON', 'url': '/the-holy-rust-book/'}, 'replies': [{'_class': 'answer', 'user': {'_class': 'user', 'display_name': 'Ella'}}]}
{'_class': 'question', 'course': {'_class': 'course', 'title': 'PHP ULTIMATE WEB GUIDE', 'url': '/php-to-the-world/'}, 'replies': [{'_class': 'answer', 'user': {'_class': 'user', 'display_name': 'Rick_Sanchez'}}]}
{'_class': 'question', 'course': {'_class': 'course', 'title': 'Javascript - The Complete Guide', 'url': '/javascript291/'}, 'replies': [{'_class': 'answer', 'user': {'_class': 'user', 'display_name': 'Morty'}}]}
But now my problem is.. if I want to access the replies
key in the question_data
key, I keep getting the last replies
value and not all of the values.
i.e:
replies_access = question_data['replies']
returns:
[{'_class': 'answer', 'user': {'_class': 'user', 'display_name': 'Morty'}}]
Shouldn't replies_access = question_data['replies']
return all of the values that have the replies
key and not only the last value? Or am i doing something wrong?
Upvotes: 0
Views: 147
Reputation: 107
replies_access = question_data['replies']
will assign the value of the current question_data to replies_access. Since you're in a for loop, you keep assigning the next replies value to replies_access. Therefore in the last iteration you assign the last replies object and that's what you get when you print. Instead, you can make a list comprehension or use append.
replies_access = list()
for question_data in question_access:
replies_access.append(question_data['replies'])
or
replies_access = [ question_data['replies'] for question_data in question_access]
Upvotes: 1
Reputation: 54148
One question_data
is one element only from the values, so accessing ['replies']
gives you one for sure, and if you keep saving it in the same variable, at the end, only the last one remain
You may use a list to collect the replies during the loop
replies = []
for question_data in question_access:
replies_access = question_data['replies']
replies.append(replies_access)
print(replies)
[[{'_class': 'answer', 'user': {'_class': 'user', 'display_name': 'Maximilian'}}],
[{'_class': 'answer', 'user': {'_class': 'user', 'display_name': 'Ella'}}],
[{'_class': 'answer', 'user': {'_class': 'user', 'display_name': 'Rick_Sanchez'}}],
[{'_class': 'answer', 'user': {'_class': 'user', 'display_name': 'Morty'}}]]
Upvotes: 2