Replace single with double quotes in a List in Python

avatar

Borislav Hadzhiev

Last updated: Sep 8, 2022

banner

Photo from Unsplash

Replace single with double quotes in a List in Python #

Use the json.dumps() method to replace single with double quotes in a list, e.g. json_str = json.dumps(my_list). The json.dumps() method will convert the list to a JSON string and will wrap all strings in the list in double quotes.

main.py
import json my_list = ['bobby', 'hadz', 'com'] # ✅ Replace single with double quotes in a list (json.dumps()) json_str = json.dumps(my_list) print(json_str) # 👉️ ["bobby", "hadz", "com"] # ------------------------------------------- # ✅ Replace single with double quotes in a list (str.replace()) my_list = ["'bobby'", "'hadz'", "'com'"] result = [item.replace("'", '"') for item in my_list] print(result) # 👉️ ['"bobby"', '"hadz"', '"com"']

The first example uses the json.dumps() method to replace the single quotes in a list with double quotes.

The json.dumps method converts a Python object to a JSON formatted string.

main.py
import json my_list = ['bobby', 'hadz', 'com'] # 👇️ json.dumps() converts a Python object to JSON string json_str = json.dumps(my_list) print(json_str) # 👉️ ["bobby", "hadz", "com"] print(type(json_str)) # 👉️ <class 'str'>
The strings in a valid JSON object are wrapped in double quotes, so all strings in the list automatically get wrapped in double quotes.

The json.dumps() method returns a string that is the JSON representation of the list.

If you need to convert the string to a native Python list, use the json.loads() method.

main.py
import json my_list = ['bobby', 'hadz', 'com'] json_str = json.dumps(my_list) print(json_str) # 👉️ ["bobby", "hadz", "com"] native_list = json.loads(json_str) print(native_list) # 👉️ ['bobby', 'hadz', 'com']

The json.loads method parses a JSON string into a native Python object.

Alternatively, you can call the str.replace() method on each item in the list.

Replace single with double quotes in a List using str.replace() #

To replace single with double quotes in a list:

  1. Use a list comprehension to iterate over the list.
  2. Use the str.replace() method to replace single with double quotes in each string.
  3. The strings in the new list will only contain double quotes.
main.py
my_list = ["'bobby'", "'hadz'", "'com'"] result = [item.replace("'", '"') for item in my_list] print(result) # 👉️ ['"bobby"', '"hadz"', '"com"']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the str.replace() method to replace all single quotes in the string with double quotes.

main.py
my_str = "bobby 'hadz' com" result = my_str.replace("'", '"') print(result) # 👉️ bobby "hadz" com

Notice that we alternate between double and single quotes in the string.

This is necessary to avoid terminating the string prematurely.

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

The strings in the new list only contain double quotes.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.