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

IndexError: list index out of range while converting list into dictionary

input_list=[[('This civilisation flourished between 2500 __________________ and 1900 __________________ in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.',
   'This civilisation flourished between 2500 BCE and 1900 BCE in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.'),
  'BCE'],
 [('Around the same time IndoAryan tribes moved into the Punjab from __________________ Asia in several waves of migration.',
   'Around the same time IndoAryan tribes moved into the Punjab from Central Asia in several waves of migration.'),
  'Central']]


output= [{'question': 'This civilisation flourished between 2500 __________________ and 1900 __________________ in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.',
   'context':'This civilisation flourished between 2500 BCE and 1900 BCE in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.',
  'answer': 'BCE'},
 {'question': 'Around the same time IndoAryan tribes moved into the Punjab from __________________ Asia in several waves of migration.',
  'context': 'Around the same time IndoAryan tribes moved into the Punjab from Central Asia in several waves of migration.',
  'answer': 'Central'}]

I have tried the above method but it is giving me an index error. and I think it is due to the round brackets.

generated_answer=[{'question': l[0], 'context':l[1],'answer': l[2]} for l in input_list]

so how can I get output as shown?

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 :

Your data has pattern like this
[[('question','context'),'answer'],...]

Try this

input_list=[[('This civilisation flourished between 2500 __________________ and 1900 __________________ in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.',
   'This civilisation flourished between 2500 BCE and 1900 BCE in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.'),
  'BCE'],
 [('Around the same time IndoAryan tribes moved into the Punjab from __________________ Asia in several waves of migration.',
   'Around the same time IndoAryan tribes moved into the Punjab from Central Asia in several waves of migration.'),
  'Central']]
generated_answer=[{'question': l[0][0], 'context':l[0][1],'answer': l[1]} for l in input_list]
print(generated_answer)

Output

[{'question': 'This civilisation flourished between 2500 __________________ and 1900 __________________ in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.',
 'context': 'This civilisation flourished between 2500 BCE and 1900 BCE in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.',
 'answer': 'BCE'}, 
{'question': 'Around the same time IndoAryan tribes moved into the Punjab from __________________ Asia in several waves of migration.',
 'context': 'Around the same time IndoAryan 
tribes moved into the Punjab from Central Asia in several waves of migration.', 
'answer': 'Central'}]
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