Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading