. Advertisement .
..3..
. Advertisement .
..4..
Knowing how to split a string by comma in Python can come in handy when you have to get values from it. This tutorial will walk you through this task by using the split() method.
Split A String By Comma In Python
split()
The solution most developers need when they want to split a Python string is str.split(). The syntax of this method is as follows:
split(sep, maxsplit)
In which sep is the delimiter string split() will use to split the string. If you provide a value to the maxpslit parameter, the method will provide at most that number of splits.
The following example demonstrates how you can use split() with a string in Python:
>>> mystr = 'ITTutoria,Stack OverFlow,Quora'
>>> print(mystr.split(','))
['ITTutoria', 'Stack OverFlow', 'Quora']
Here we use a comma as the delimiter character. The split() method returns a list containing substrings divided by a comma in the original string.
Remember that by default, the sep parameter doesn’t have any value. If you don’t specify it, split() will use a different algorithm to split your string:
>>> mystr = 'ITTutoria,Stack OverFlow,Quora'
>>> print(mystr.split())
['ITTutoria,Stack', 'OverFlow,Quora']
Splitting Empty Strings
In this case, split() counts consecutive whitespace as one separator, and the returned list will have no empty substrings at the end or start when the original string contains trailing or leading whitespace.
That is why when you split a string of only whitespace or an empty string without specifying a value for the sep parameter, split() will return an empty list:
>>> print(''.split())
[]
This stands in stark contrast to when you split empty strings with a specified delimiter. For instance, this example will return a list of one element (albeit also an empty string itself):
>>> print(''.split(','))
['']
You should bear this crucial difference in mind when you expect to split some empty strings. The results, in particular the number of elements in the returned array, will vary depending on the separator you provide.
Number Of Splits
You can control the number of splits by using the maxpslit parameter. For instance, this example tells split() to split a string just once:
>>> mystr = 'ITTutoria,Stack OverFlow,Quora'
>>> print(mystr.split(',', 1))
['ITTutoria', 'Stack OverFlow,Quora']
Remember that this parameter is about the splits done by the method, not the number of returned elements. It means when you set maxsplit to 1, split() will return a list of 2 elements (at most).
What separates these two substrings is the first delimiter (the comma) split() encountered when going through the string. While there is a comma towards the end of the string, split() ignores it because the number of splits has reached the limit.
Consecutive Separators
When there are consecutive delimiters in the string, split() doesn’t deem them as one. Instead, the method treats each of them individually.
When two delimiters sit next to next other, split() considers there is an empty substring between them and returns it to the final array:
>> mystr = 'ITTutoria,,,Stack OverFlow,,Quora'
>> print(mystr.split(','))
['ITTutoria', '', '', 'Stack OverFlow', '', 'Quora']
There are two ways to solve this:
mystr = 'ITTutoria,,,Stack OverFlow,,Quora'
splitArray = mystr.split(',')
print(splitArray)
print([x for x in splitArray if x])
print(list(filter(None, splitArray)))
Output:
['ITTutoria', '', '', 'Stack OverFlow', '', 'Quora']
['ITTutoria', 'Stack OverFlow', 'Quora']
['ITTutoria', 'Stack OverFlow', 'Quora']
The first solution is using list comprehensions. It will build a new array from all the elements in the original arrays that match the condition ‘if x’.
In Python, empty strings are falsy values, meaning they evaluate to false when put in a Boolean comparison. This isn’t the case with non-empty strings. When used with the above condition, the list comprehension uses only non-empty strings to create the new array.
Note: you can also make a deep copy of a list or create a list of lists with list comprehensions.
The second option you have is the filter() function. It can build an iterator from elements of another iterable, such as our list. Its syntax is:
filter(function, iterable)
When the function parameter is set to None (as above), filter() will remove ‘false’ elements from the iterable. That is why all the empty strings have been removed from our string.
Summary
You can split a string by comma in Python with the split() function, which allows you to set the delimiter string and the maximum number of splits.
Leave a comment