In Dataquest Reddit API challenge, you are given a list of nested dictionaries. Each dictionary is a post from from Reddit, and you need to find the most up-voted one. The up-vote value is stores in each dictionary['ups'] key.

The website gives the following as answer:

python_top_articles = python_top["data"]["children"]
most_upvoted = ""
most_upvotes = 0
for article in python_top_articles:
    ar = article["data"]
    if ar["ups"] >= most_upvotes:
        most_upvoted = ar["id"]
        most_upvotes = ar["ups"]

Is there a more pythonic way of doing this, for example returning the max(dict['ups']) value? My attempt is as following:

 max_ups = [max(value) for key,value in items.items() \\
            for items in python_top_articles if key == 'ups']
 #attempt, return error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.