List into json format using Python
Question: I have a list like below...

    
    
    lst = ['dosa','idly','sambar']
    

i need to convert the above data to below format by using Python.

    
    
    [{'menuitem': 'idly'},
     {'menuitem': 'dosa'},
     {'menuitem': 'sambar'},
    ]
    

Thanks.


Answer: Using [list
comprehension](https://docs.python.org/3/tutorial/datastructures.html#list-
comprehensions), make a list of dictionaries:

    
    
    >>> lst = ['dosa','idly','sambar']
    >>> [{'menuitem': x} for x in lst]
    [{'menuitem': 'dosa'}, {'menuitem': 'idly'}, {'menuitem': 'sambar'}]
    

Then, convert it to json using
[`json.dumps`](https://docs.python.org/3/library/json.html#json.dumps):

    
    
    >>> import json
    >>> json.dumps([{'menuitem': x} for x in lst])
    '[{"menuitem": "dosa"}, {"menuitem": "idly"}, {"menuitem": "sambar"}]'
    

