Home > Back-end >  How to iterate through a dict of tuples from Stack Overflow api
How to iterate through a dict of tuples from Stack Overflow api

Time:10-11

I am using the Stack Overflow api to search for questions by passing a query.

I am using the following code:

def search_query_stackoverflow(query):
    title_search_string =  query
    SITE = StackAPI('stackoverflow')
    questions = SITE.fetch('search/advanced', title = title_search_string)
    print(questions)

I would like to get or be able to manipulate everything inside the key "items". For example I would like to get the key title and link. I get the following in dict format. I know that it is a dict of tuples, but I can not get the keys title and link from the dict inside the tuples.

{'backoff': 0,
 'has_more': False,
 'items': [{'answer_count': 1,
            'content_license': 'CC BY-SA 4.0',
            'creation_date': 1633328463,
            'is_answered': False,
            'last_activity_date': 1633372180,
            'last_edit_date': 1633335100,
            'link': 'https://stackoverflow.com/questions/69431652/insert-a-position-in-a-linked-list-java',
            'owner': {'display_name': 'wonderfulLife',
                      'link': 'https://stackoverflow.com/users/17068499/wonderfullife',
                      'profile_image': 'https://www.gravatar.com/avatar/d769e5190595588a327bc10fa491d504?s=128&d=identicon&r=PG',
                      'reputation': 1,
                      'user_id': 17068499,
                      'user_type': 'registered'},
            'question_id': 69431652,
            'score': -2,
            'tags': ['java', 'linked-list', 'singly-linked-list'],
            'title': 'Insert a position in a linked list Java',
            'view_count': 44},
           {'answer_count': 1,
            'content_license': 'CC BY-SA 4.0',
            'creation_date': 1623062466,
            'is_answered': True,
            'last_activity_date': 1623063969,
            'link': 'https://stackoverflow.com/questions/67870147/insert-generic-element-at-a-specific-index-in-doubly-linked-list-java',
            'owner': {'display_name': 'nooralight',
                      'link': 'https://stackoverflow.com/users/14570244/nooralight',
                      'profile_image': 'https://www.gravatar.com/avatar/f961618fb740715e2a03edc1d0f4c8e0?s=128&d=identicon&r=PG&f=1',
                      'reputation': 39,
                      'user_id': 14570244,
                      'user_type': 'registered'},
            'question_id': 67870147,
            'score': 0,
            'tags': ['java', 'doubly-linked-list'],
            'title': 'Insert generic element at a specific index in Doubly '
                     'linked List java',
            'view_count': 44},
           {'answer_count': 1,
            'content_license': 'CC BY-SA 4.0',
            'creation_date': 1621429048,
            'is_answered': False,
            'last_activity_date': 1621441007,
            'link': 'https://stackoverflow.com/questions/67603746/how-do-i-delete-the-last-occurrence-in-a-circular-linked-list-java',
            'owner': {'display_name': 'Parasite ',
                      'link': 'https://stackoverflow.com/users/15842418/parasite',
                      'profile_image': 'https://lh3.googleusercontent.com/a/AATXAJx2qqQKeuP3GkdbYb3CCWssnyQobuoy3uJ69rF-=k-s128',
                      'reputation': 37,
                      'user_id': 15842418,
                      'user_type': 'registered'},
            'question_id': 67603746,
            'score': 0,
            'tags': ['java', 'list'],
            'title': 'How do I delete the last occurrence in a circular linked '
                     'list? (java)',
            'view_count': 51},
           {'accepted_answer_id': 67493385,
            'answer_count': 2,
            'content_license': 'CC BY-SA 4.0',
            'creation_date': 1620759947,
            'is_answered': True,
            'last_activity_date': 1620763478,
            'last_edit_date': 1620760390,
            'link': 'https://stackoverflow.com/questions/67492919/deleting-all-occurrences-in-a-circular-linked-list-java',
            'owner': {'display_name': 'Parasite ',
                      'link': 'https://stackoverflow.com/users/15842418/parasite',
                      'profile_image': 'https://lh3.googleusercontent.com/a/AATXAJx2qqQKeuP3GkdbYb3CCWssnyQobuoy3uJ69rF-=k-s128',
                      'reputation': 37,
                      'user_id': 15842418,
                      'user_type': 'registered'},
            'question_id': 67492919,
            'score': 3,
            'tags': ['java', 'list', 'abstract-class', 'circular-list'],
            'title': 'Deleting all occurrences in a circular linked list. '
                     '(java)',
            'view_count': 54},
           {'accepted_answer_id': 66575117,
            'answer_count': 1,
            'content_license': 'CC BY-SA 4.0',
            'creation_date': 1615422441,
            'is_answered': True,
            'last_activity_date': 1615426254,
            'link': 'https://stackoverflow.com/questions/66574824/triple-cut-of-a-circular-doubly-linked-list-java',
            'owner': {'display_name': 'hexaquark',
                      'link': 'https://stackoverflow.com/users/12985747/hexaquark',
                      'profile_image': 'https://lh3.googleusercontent.com/a-/AAuE7mCEOfSH2ceAoOtaNgVnp7ZeozLKmlWx1EEhlLyK0A=k-s128',
                      'reputation': 504,
                      'user_id': 12985747,
                      'user_type': 'registered'},
            'question_id': 66574824,
            'score': 0,
            'tags': ['java', 'doubly-linked-list', 'circular-list'],
            'title': 'Triple-cut of a circular doubly linked list, Java',
            'view_count': 162},
           {'accepted_answer_id': 40175374,
            'answer_count': 3,
            'content_license': 'CC BY-SA 3.0',
            'creation_date': 1477048590,
            'is_answered': True,
            'last_activity_date': 1612869999,
            'last_edit_date': 1477063453,
            'link': 'https://stackoverflow.com/questions/40175252/priority-queue-vs-linked-list-java',
            'owner': {'display_name': 'vikkz',
                      'link': 'https://stackoverflow.com/users/7052778/vikkz',
                      'profile_image': 'https://www.gravatar.com/avatar/72490089dcfe85b71c97179f8118c9e2?s=128&d=identicon&r=PG&f=1',
                      'reputation': 45,
                      'user_id': 7052778,
                      'user_type': 'registered'},
            'question_id': 40175252,
            'score': 4,
            'tags': ['java',
                     'algorithm',
                     'data-structures',
                     'graph',
                     'graph-algorithm'],
            'title': 'priority queue vs linked list java',
            'view_count': 7665},
           {'accepted_answer_id': 5607646,
            'answer_count': 7,
            'content_license': 'CC BY-SA 3.0',
            'creation_date': 1302377340,
            'is_answered': True,
            'last_activity_date': 1611486685,
            'last_edit_date': 1377401887,
            'link': 'https://stackoverflow.com/questions/5607292/interview-remove-loop-in-linked-list-java',
            'owner': {'accept_rate': 91,
                      'display_name': 'SuperMan',
                      'link': 'https://stackoverflow.com/users/286629/superman',
                      'profile_image': 'https://www.gravatar.com/avatar/ce742ecb3857f142e886c334f8a7c7cf?s=128&d=identicon&r=PG',
                      'reputation': 3456,
                      'user_id': 286629,
                      'user_type': 'registered'},
            'question_id': 5607292,
            'score': 62,
            'tags': ['java', 'data-structures', 'linked-list'],
            'title': 'Interview: Remove Loop in linked list - Java',
            'view_count': 26845},
           {'answer_count': 2,
            'content_license': 'CC BY-SA 4.0',
            'creation_date': 1611164682,
            'is_answered': False,
            'last_activity_date': 1611176658,
            'link': 'https://stackoverflow.com/questions/65814815/doubly-linked-list-java-method-for-inserting-at-the-beginning',
            'owner': {'display_name': 'bdd0',
                      'link': 'https://stackoverflow.com/users/14470647/bdd0',
                      'profile_image': 'https://www.gravatar.com/avatar/274c7cf8d0cf18093647adaca4602b04?s=128&d=identicon&r=PG&f=1',
                      'reputation': 59,
                      'user_id': 14470647,
                      'user_type': 'registered'},
            'question_id': 65814815,
            'score': -1,
            'tags': ['java', 'linked-list'],
            'title': 'Doubly linked list java method for inserting at the '
                     'beginning',
            'view_count': 36}],
 'page': 2,
 'quota_max': 300,
 'quota_remaining': 180,
 'total': 0}
 ...

CodePudding user response:

The type of the value of the key items inside the dict is list so you can manipulate it using list comprehension. For example -

questions['items'] = [{'link':item['link']} for item in questions['items']
  • Related