Replace Comma with Space in List in Python

In this post, we will see how to replace comma with space in the list.

How to replace comma with space in list in Python

A list is one of the fundamental collection objects in Python. It can store multiple elements and we can access these elements using their respective index. When we display a list, every element is separated with a comma.

In this article, we will discuss how to replace comma with space in list in Python. Note that we cannot directly replace the comma while displaying a list. So in our methods, we will convert the list to a string and then replace the comma with space in list in Python.

The methods are discussed below.

Using the replace() function to replace comma with space in list in Python

In Python, we can replace substrings within a string using the replace() function. Using this function, we can replace comma with space in list in Python. It returns a new string with the substituted substring.

But first, remember that we need to get the string representation of the list. We can achieve this using the str() function.

See the code below.

Output:

[11 33 22 99]

In the above example, we replace comma with space in list in Python. We first converted the list to a string using the str() function. Then we replaced the commas with space using the replace() function.

Using the re.sub() function to replace comma with space in list in Python

Regular expressions are an elegant way to match parts of strings using regex patterns. In Python, we can use the re module to work with regular expressions.

The re.sub() function can be used to replace a given substring that matches some regex pattern. We can use it to replace comma with space in list in Python. For our case, we use it without any regex pattern and directly mention commas in the function.

See the code below.

Output:

[11 33 22 99]

Using the split() and join() functions to replace comma with space in list in Python.

The split() function is used to split a string into a list of substrings by splitting it based on a character. The join() function is used in Python to combine the elements from an iterable as a string.

First, we will convert the list to a string as done in previous methods. We will then split the string based on the comma using the split() function. Then the elements are combined using the join() function. The space is used as a separator character in the join() function.

See the code below.

Output:

[11 33 22 99]

In the above example, we first split the string to a list and then combine it again to replace comma with space in list in Python.

Conclusion

To conclude, we discussed how to replace comma with space in list in Python. The first two methods were very straightforward.

We converted the list to a string and replaced the comma with space using the replace() and re.sub() functions.

In the final method, we used two functions to split the string into a list again and then use the join() function to combine the elements using the space character as the separator.

That’s all about how to replace comma with space in List in Python

Was this post helpful?

Related Posts

  • 23 September

    Add Tuple to List in Python

    Table of ContentsAdd tuple to list in PythonWays to Add Tuple to List in Python (adding the Tuple as An Element)Using the insert() Function to Add Tuple to List in PythonUsing the append() Function to Add Tuple to List in PythonWays to Add Tuple to List in Python (adding the Tuple’s Elements to A List)Using […]

  • 19 May

    Compare list elements with each other in Python

    Table of ContentsCompare all elements in list PythonWays to compare list elements with each other in PythonUsing the for loop to compare all elements in list PythonUsing the itertools.combinations() function to compare all elements in list PythonConclusion In this post, we will see how to compare list elements with each other in Python. Compare all […]

  • 18 May

    How to Deep Copy a List in Python

    Table of ContentsWhat is a list in Python?What are shallow copy and deep copy operations in Python?Shallow CopyDeep CopyHow to deep copy a list in Python?Using the deepcopy() function from the copy module to deep copy a list in Python.Using a user-defined function to deep copy a list in Python.Using list comprehension to deep copy […]

  • 18 May

    Convert Generator to List in Python

    Table of ContentsLists and Generators in PythonWays to convert generator to list in PythonUsing the list() function to convert generator to list in PythonUsing the unpack operator * to convert generator to list in PythonUsing the list comprehension to convert generator to list in PythonUsing the append() function to convert generator to list in PythonConclusion […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • 07 January

    How to compare lists in Python

    Table of ContentsPython compare listsHow to compare lists in PythonUsing the sort() function and == operatorUsing the map() and reduce() functionsUsing the set() function and == operatorUsing the for loopUsing the set() function and - operatorUsing the collections.Counter() class and == operatorUsing the zip(), sum(), and len() functionsUsing the cmp() functionConclusion In this post, we […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.