Friday, 16 August 2013

How to create a Python dictionary with double quotes as default quote format?

How to create a Python dictionary with double quotes as default quote format?

I am trying to create a python dictionary which is to be used as a java
script var inside a html file for visualization purposes. As a requisite,
I am in need of creating the dictionary with all names inside double
quotes instead of default single quotes which Python uses. Is there an
easy and elegant way to achieve this?
couples = [
['jack', 'ilena'],
['arun', 'maya'],
['hari', 'aradhana'],
['bill', 'samantha']]
pairs = {}
for couple in couples:
pairs[couple[0]] = couple[1]
print pairs
Generated Output:
{'arun': 'maya', 'bill': 'samantha', 'jack': 'ilena', 'hari': 'aradhana'}
Expected Output:
{"arun": "maya", "bill": "samantha", "jack": "ilena", "hari": "aradhana"}
I know, json.dumps(pairs) does the job, but the dictionary as a whole is
converted into a string which isn't what I am expecting.

No comments:

Post a Comment