Explore the data structures to understand them. This was how I solved using a function.
python_top_articles is a list of 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 one_dict
Inside this dictionary, one_dict, read the value of the key ups and store as up ( up = one_dict['ups']) and the value in the key id and store as ids (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']
def most_ups(a_list):
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
return post
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[0] #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?
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'] type(python_top['data']['children']) python_top['data']['children'][0] since this is a list type(python_top['data']['children'][0]) gives a dictionary. python_top['data']['children'][0].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?