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?

Leave a Reply

Your email address will not be published. Required fields are marked *