Hi guys, currently struggling with Challenge: Working With the reddit API mission 3.
I don’t understand how I can use the documentation to understand all the different keys in the dictionary.
children key, which is inside the
data key would contain the list of posts from the
Not sure how I would of worked that out myself through the documentation.
Explore the data structures to understand them. This was how I solved using a function.
python_top_articles is a
dictionaries. So the
most_ups function takes
the list of dictionaries: python_top_articles.
for one_dict in list_ takes one dictionary from the list of dictionaries. However, this
one_dict is a
dictionary of dictionaries.
one_dict = one_dict['data'] take one dictionary from the dictionary of dictionaries with the key
data. Still call the name of this new dictionary
Inside this dictionary,
one_dict, read the value of the key
ups and store as
up = one_dict['ups']) and the value in the key
id and store as
ids` = one_dict['id']).
If the value of
up is more than the initialize maximum value
maxv, update the value in
maxv to this new value of
up, return this dictionary with the new high value. This is what the function does.
Run the function on the list of dictionaries, it returns a dictionary with the max value. Use the
id key to get the id from the dictionary.
python_top_articles = python_top['data']['children']
list_ = a_list
maxv = 0
author = None
post = None
for one_dict in list_:
one_dict = one_dict['data']
up = one_dict['ups']
ids = one_dict['id']
if up > maxv:
maxv = up
author = ids
post = one_dict
most_upvoted = most_ups(python_top_articles)['id']
Hey thanks for the reply.
Helped alot, understanding the data structure more.
Especially since python_top articles contain a dictionary with another dictionary inside.
However, within the data structure, still not sure how I would know what each key would mean even with the documents.
So for example:
first_article = python_top_articles #taking first article data first_article_data = first_article['data'] # getting first article number of up votes. upvote = first_article_data['ups']
How would I have know key
'ups' means the number of up vote?
Thanks again for your help!
What I do is that I explore the data structures first before writing any code. I use
type to see what kind of data structure the I am currently in and if it is a dictionary, I will call
keys() on it to see the keys.
python_top['data']['children'] since this is a list
type(python_top['data']['children']) gives a dictionary.
python_top['data']['children'].keys() to get its keys.
Something like this. This may not be correct because I did not run the above code.
I think the question was: how to understand the reddit API documentation? If dataquest.io didnt explain to us, for example, that ups is number of voutes, we wouldnt have find that throught API documentation by ourselves . In the mission dataquest.io gives links of the reddit API documentation but I tried to figure out the information in it and failed.
Can you kindly show me where exactly i can find the information that ups is a number of votes?
Sorry for my bad explanation. I meant where exactly in reddit API documentation is stated that ups is a number of votes?
The link is from the mission https://app.dataquest.io/m/110/challenge%3A-working-with-the-reddit-api/2/authenticating-with-the-api