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 convert a list to dictionary in python?

I have a list like this. containing question and its answer.

list=[['It has been noted that in __________________ urban cities quickly expanded rather than the slow evolution of urbanism in the northwest.',
      'Gujarat'],
     ['These traditions and cultures include Anarta Tradition c. 39501900 BC Padri Ware 36002000 BC PrePrabhas Assemblage 32002600 BC Pre Urban Harappan Sindh __________________ Pottery Burial Pottery 30002600 BC Black and Red Ware 3950900 BC Reserved Slip Ware 39501900 BC Micaceous Red Ware 26001600 BC.',
      'Type'],
     ['Later in the 18th century Gujarat came under control of the __________________ Empire who dominated the politics of India.',
      'Maratha']]

I want to convert in into given form.

output =[{'question': 'It has been noted that in __________________ urban cities quickly expanded rather than the slow evolution of urbanism in the northwest.','answer':'Gujarat'}, {'question': 'These traditions and cultures include Anarta Tradition c. 39501900 BC Padri Ware 36002000 BC PrePrabhas Assemblage 32002600 BC Pre Urban Harappan Sindh __________________ Pottery Burial Pottery 30002600 BC Black and Red Ware 3950900 BC Reserved Slip Ware 39501900 BC Micaceous Red Ware 26001600 BC.','answer':'Type'}, {'question': 'Later in the 18th century Gujarat came under control of the __________________ Empire who dominated the politics of India.','answer':'Maratha'}

Is it possible ? If yes , then how ?

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 :

A list comprehension will suffice:

[{'question': q, 'answer': a} for q, a in list]

This outputs:

[{'question': 'It has been noted that in __________________ urban cities '
              'quickly expanded rather than the slow evolution of urbanism in '
              'the northwest.',
  'answer': 'Gujarat'},
 {'question': 'These traditions and cultures include Anarta Tradition c. '
              '39501900 BC Padri Ware 36002000 BC PrePrabhas Assemblage '
              '32002600 BC Pre Urban Harappan Sindh __________________ Pottery '
              'Burial Pottery 30002600 BC Black and Red Ware 3950900 BC '
              'Reserved Slip Ware 39501900 BC Micaceous Red Ware 26001600 BC.',
  'answer': 'Type'},
 {'question': 'Later in the 18th century Gujarat came under control of the '
              '__________________ Empire who dominated the politics of India.',
  'answer': 'Maratha'}]
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