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 find a key from a list of dictionary?

I have a list like this.

MCQ = [[{'answer': [{'answer': 'Indian', 'correct': False},
    {'answer': 'Brahmanism', 'correct': False},
    {'answer': 'Hindu', 'correct': False},
    {'answer': 'Vedic Brahmanism', 'correct': True}],
   'question': 'What religions were synthesised with the preexisting cultures of the subcontinent?'},
  {'answer': [{'answer': 'the Maurya Empire', 'correct': True},
    {'answer': 'the Gupta Empire', 'correct': False},
    {'answer': 'Punjab', 'correct': False},
    {'answer': 'Central Asia', 'correct': False}],
   'question': 'What Empire ruled the Indian subcontinent?'},
  {'answer': [{'answer': 'the Indus Valley Civilization', 'correct': True},
    {'answer': 'BCE', 'correct': False},
    {'answer': 'Wootz', 'correct': False},
    {'answer': 'Prakrit', 'correct': False}],
   'question': 'What was the first civilization of the Old world?'},
  {'answer': [{'answer': 'Southeast Asia', 'correct': False},
    {'answer': 'Vedic', 'correct': True},
    {'answer': 'Asia', 'correct': False},
    {'answer': 'the Indus Valley', 'correct': False}],
   'question': 'What religions were associated with the urbanisation of Greater Magadha?'},
  {'answer': [{'answer': 'the next 1500 years', 'correct': False},
    {'answer': 'between 73000 and 55000 years ago', 'correct': False},
    {'answer': 'the second millennium', 'correct': True},
    {'answer': 'the 3rd century', 'correct': False}],
   'question': 'When did the Indus Valley begin to disappear?'}]]

I want to differentiate a list like this

output = [{'question': 'What religions were synthesised with the preexisting cultures of the subcontinent?','options':['Indian','Brahmanism','Hindu','Vedic Brahmanism'],'answer':'Vedic Brahmanism'},{'question': 'What Empire ruled the Indian subcontinent?','options':['the Maurya Empire','the Gupta Empire','Punjab','Central Asia'],'answer':'the Maurya Empire'}]

Note 1: options field (in output) contain all the answer field in MCQ.

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

EX. [{'answer': [{'answer': 'Indian', 'correct': False},
{'answer': 'Brahmanism', 'correct': False},
{'answer': 'Hindu', 'correct': False},
{'answer': 'Vedic Brahmanism', 'correct': True}] (from MCQ)
**options** = Indian , Brahmanism , Hindu , Vedic Brahmanism (in output)

Note 2 : answer field (in output) contain True value in answer -> answer -> ‘correct’: True (in MCQ)

EX. [{'answer': [{'answer': 'Indian', 'correct': False},
    {'answer': 'Brahmanism', 'correct': False},
    {'answer': 'Hindu', 'correct': False},
    {'answer': 'Vedic Brahmanism', 'correct': True}] (from MCQ)
    answer = Vedic Brahmanism (in output)

>Solution :

One solution:

output = []
for e in MCQ:
    for d in e:
        row = {"options": []}
        for a in d["answer"]:
            row["options"].append(a["answer"])
            if a["correct"]:
                row["answer"] = a["answer"]
        row["question"] = d["question"]
        output.append(row)
print(output)

Output

[{'answer': 'Vedic Brahmanism',
  'options': ['Indian', 'Brahmanism', 'Hindu', 'Vedic Brahmanism'],
  'question': 'What religions were synthesised with the preexisting cultures of the subcontinent?'},
 {'answer': 'the Maurya Empire',
  'options': ['the Maurya Empire', 'the Gupta Empire', 'Punjab', 'Central Asia'],
  'question': 'What Empire ruled the Indian subcontinent?'},
 {'answer': 'the Indus Valley Civilization',
  'options': ['the Indus Valley Civilization', 'BCE', 'Wootz', 'Prakrit'],
  'question': 'What was the first civilization of the Old world?'},
 {'answer': 'Vedic',
  'options': ['Southeast Asia', 'Vedic', 'Asia', 'the Indus Valley'],
  'question': 'What religions were associated with the urbanisation of Greater Magadha?'},
 {'answer': 'the second millennium',
  'options': ['the next 1500 years', 'between 73000 and 55000 years ago', 'the second millennium', 'the 3rd century'],
  'question': 'When did the Indus Valley begin to disappear?'}]
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