How to replace a specific word in a list with another word?

Question:

input=[{'options': ['IndoAryan', 'Vedic Brahmanism', 'the 3rd century', '4th'], 'answer': '4th', 'question': 'What religions were synthesised with the preexisting cultures of the subcontinent?', 'context': 'During this period aspects of Indian civilisation administration culture and religion Hinduism and Buddhism spread to much of Asia while kingdoms in southern India had maritime business links with the Middle East and the Mediterranean.', 'rank': 45},{'options': ['Classical', 'the Maurya Empire', 'Wootz', 'BCE'], 'answer': 'BCE', 'question': 'What Empire ruled the Indian subcontinent?', 'context': 'During this period aspects of Indian civilisation administration culture and religion Hinduism and Buddhism spread to much of Asia while kingdoms in southern India had maritime business links with the Middle East and the Mediterranean.', 'rank': 9}]

I want to replace the answer with the None of the above from the options.

Output = [{'options': ['IndoAryan', 'Vedic Brahmanism', 'the 3rd century', **'None of the above'**], 'answer': '4th', 'question': 'What religions were synthesised with the preexisting cultures of the subcontinent?', 'context': 'During this period aspects of Indian civilisation administration culture and religion Hinduism and Buddhism spread to much of Asia while kingdoms in southern India had maritime business links with the Middle East and the Mediterranean.', 'rank': 45},{'options': ['Classical', 'the Maurya Empire', 'Wootz', '**None of the above**'], 'answer': 'BCE', 'question': 'What Empire ruled the Indian subcontinent?', 'context': 'During this period aspects of Indian civilisation administration culture and religion Hinduism and Buddhism spread to much of Asia while kingdoms in southern India had maritime business links with the Middle East and the Mediterranean.', 'rank': 9}]

In answer is ‘4th’ in the first dictions , so from the options replace ‘4th’ with the ‘None of above’

Asked By: dilip kukadiya

||

Answers:

You can iterate through every option of every question and change the option that is answer to "None of the above"

questions=[{'options': ['IndoAryan', 'Vedic Brahmanism', 'the 3rd century', '4th'], 'answer': '4th', 'question': 'What religions were synthesised with the preexisting cultures of the subcontinent?', 'context': 'During this period aspects of Indian civilisation administration culture and religion Hinduism and Buddhism spread to much of Asia while kingdoms in southern India had maritime business links with the Middle East and the Mediterranean.', 'rank': 45},{'options': ['Classical', 'the Maurya Empire', 'Wootz', 'BCE'], 'answer': 'BCE', 'question': 'What Empire ruled the Indian subcontinent?', 'context': 'During this period aspects of Indian civilisation administration culture and religion Hinduism and Buddhism spread to much of Asia while kingdoms in southern India had maritime business links with the Middle East and the Mediterranean.', 'rank': 9}]

# iterate every question
for question in questions:
    # control every option
    for option_index, option in enumerate(question["options"]):
        # if option is right answer
        if option == question["answer"]:
            # change option to None of the above
            question["options"][option_index] = "None of the above"

print(questions)
Answered By: Alper
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.