Home > Software engineering >  RESOLVED - How do I reference inside of a Python dictionary's dictionary?
RESOLVED - How do I reference inside of a Python dictionary's dictionary?

Time:10-11

This post has been resolved

How do I reference the topic key in the questions? I'd like to search my list for all questions that have a specific topic and then return all of the information about a random question.

However, when using the code below, I get an error saying that:

    if question['topic'] == topic:
TypeError: 'int' object is not subscriptable

However, if I try changing the integers to strings, I get an error saying that they must be integers. How do I solve this?

Code - This code is supposed to add all of the questions of a specific topic to a list so that I can use a random function inside of the list to generate the random question.

def random_question_in_topic(topic: str):
    topic_questions = []
    for question in questions:
        if question['topic'] == topic:
            topic_questions.append(question)
    
    print(topic_questions)

Extract from my dictionary.(Total of 30 items)

questions = {
    1 : {
        'type': 'memory',
        "topic": "literature",
        "question": "Who wrote Twilight series of novels?",
        "answer": "stephenie meyer",
    },
    2 : {
        'type': 'memory',
        "topic": "literature",
        "question": "What was Frankenstein's first name?",
        "answer": "jolly roger",
    },
    3 : {
        'type': 'memory',
        "topic": "general",
        "question": "What is the only anagram of the word 'english'?",
        "answer": "shingles",
    },
    4 : {
        'type': 'memory',
        "topic": "general",
        "question": "Gala, Jonagold and Pink Lady are varieties of which fruit?",
        "answer": "apple",
    }
}

Thanks in advance!

I'm using Python 3.8.12 on Replit.

CodePudding user response:

for question in questions.values():
        if question['topic'] == 'history':
            print(question)
            topic_questions.append(question)

Thank you commentors on the original post.

CodePudding user response:

The error occurs since when you iterate over a dictionary, you are iterating over its keys. Let's look at it closely.

If you run the following code

for question in questions:
    print(question)

...you will get the following output:

1
2
3
4

So, instead of iterating over keys, you might want to iterate over the values of the dictionary instead. For this purpose you may use the values() method of dict object. So, if you try the following code:

for question in questions.values():
    print(question)

...you will get the output as:

{'type': 'memory', 'topic': 'literature', 'question': 'Who wrote Twilight series of novels?', 'answer': 'stephenie meyer'}
...

Now, the variable question is an object which you can use indexing upon, like, question['topic']. So, the code could be re-written as:

def random_question_in_topic(topic: str):
    topic_questions = []
    for question in questions.values():
        if question['topic'] == topic:
            topic_questions.append(question)
    
    print(topic_questions)
  • Related