how to convert a list to dictionary in python?

OpenEyes VO

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 ?

Raymond Hettinger

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'}]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

python dictionary and list: how to convert it?

How to convert list to dictionary in Python?

How to convert list of string to list of dictionary in Python

How to convert list of list to dictionary in Python?

How to convert python list to python dictionary with sequence

How to convert a list of tuples to a dictionary of dictionaries in Python?

How do I convert list into dictionary in Python?

How to convert unciode to list of dictionary in python

How to convert String values in a dictionary to a list in python?

How to convert list of string to dictionary in python

Python 3 - How to convert a string to a list of dictionary

How to convert dictionary list to custom objects in Python

How to convert list to dictionary in python with key value?

how to convert list of dictionary into string python

How convert list of dictionaries to a dictionary of dictionaries in Python

Convert a dictionary to a list - Python

Convert a list to a dictionary in Python

python convert list to dictionary

How to convert dictionary to pandas DataFrame in Python when dictionary value is a List?

How to convert every dictionary in a list to a nested dictionary in python?

How to convert list to dictionary

How to convert dictionary list?

How to convert this list into a dictionary

How to convert list into dictionary

How to convert list of list to dictionary

how to convert 2 dimensional list into list of dictionary in python?

Convert python dictionary to python list

Python: Convert list to dictionary with indexes

Convert a List of into a dictionary in Python 3.8