Python print dict with double quotes

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 = dict[couples]
    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.

P.S.: Is there an alternate way to do this with using json, since I am dealing with nested dictionaries.

I'm sure you can and I'm sure it involves hacking away at some C Internals. It's just how Python represents strings when the string doesn't contain any single quotes inside of it.

strs = ["Fred is dead", '"Hello," she said', "Fred's head.", 'Fred meets Ned.']
print[strs]
>>> ['Fred is dead', '"Hello," she said', "Fred's head", 'Fred meets Ned.']

The only one that isn't single quoted is the one that actually contains an apostrophe. However, unlike say PHP, single and double quotes have no difference.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given dictionary with string keys, remove double quotes from it.

    Input : test_dict = {‘”Geeks”‘ : 3, ‘”g”eeks’ : 9} 
    Output : {‘Geeks’: 3, ‘geeks’: 9} 
    Explanation : Double quotes removed from keys.

    Input : test_dict = {‘”Geeks”‘ : 3} 
    Output : {‘Geeks’: 3} 
    Explanation : Double quotes removed from keys. 
     

    Method #1 :  Using dictionary comprehension + replace[]

    The combination of above functionalities can be used to solve this problem. In this, we perform removal of double quotes using replace[] with empty string. The dictionary comprehension is used for remaking dictionary.

    Python3

    test_dict = {'"Geeks"' : 3, '"is" for' : 5, '"g"eeks' : 9}

    print["The original dictionary is : " + str[test_dict]]

    res = {key.replace['"', '']:val for key, val in test_dict.items[]}

    print["The dictionary after removal of double quotes : " + str[res]]

    Output

    The original dictionary is : {'"Geeks"': 3, '"is" for': 5, '"g"eeks': 9}
    The dictionary after removal of double quotes : {'Geeks': 3, 'is for': 5, 'geeks': 9}

    Method #2 : Using re.sub[] + dictionary comprehension

    The combination of above functions is also an alternative to solve this task. In this, we employ regex to solve the problem. 

    Python3

    import re

    test_dict = {'"Geeks"' : 3, '"is" for' : 5, '"g"eeks' : 9}

    print["The original dictionary is : " + str[test_dict]]

    res = {re.sub[r'"', '', key]: val for key, val in test_dict.items[]}

    print["The dictionary after removal of double quotes : " + str[res]]

    Output

    The original dictionary is : {'"Geeks"': 3, '"is" for': 5, '"g"eeks': 9}
    The dictionary after removal of double quotes : {'Geeks': 3, 'is for': 5, 'geeks': 9}


    How do you remove double quotes from a dictionary in Python?

    Method #1 : Using dictionary comprehension + replace[] In this, we perform removal of double quotes using replace[] with empty string. The dictionary comprehension is used for remaking dictionary.

    Can Python dictionary have single quotes?

    The dictionary doesn't contain single quotes.

    How do you convert single quotes to double quotes in Python?

    Replace Single Quotes With Double Quotes in Python.
    Method 1: Using the str.replace[] function..
    Method 2: Using the sub[] function in re package..
    Method 3: Using translate[] function in Python..
    Method 4: Using the JSON module..

    What is difference between single quote and double quote in Python?

    Generally, double quotes are used for string representation and single quotes are used for regular expressions, dict keys or SQL. Hence both single quote and double quotes depict string in python but it's sometimes our need to use one type over the other.

    Chủ Đề