Last updated: Apr 9, 2024
Reading timeยท6 min
Use the str.split()
method to convert a comma-separated string to a list,
e.g. my_list = my_str.split(',')
.
The str.split()
method will split the string on each occurrence of a comma
and will return a list containing the results.
my_str = 'bobby,hadz,com' my_list = my_str.split(',') print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com']
If you need to convert a List to a comma-separated string, click on the following subheading:
We used the str.split()
method to convert a comma-separated string to a list.
The str.split() method splits the string into a list of substrings using a delimiter.
The method takes the following 2 parameters:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done (optional) |
str.split()
method, it splits the input string on one or more whitespace characters.If the separator is not found in the string, a list containing only 1 element is returned.
print('one'.split(',')) # ๐๏ธ ['one']
Make sure to adjust the separator if your string has a different delimiter.
my_str = 'bobby, hadz, com' my_list = my_str.split(', ') print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com']
The string in the example has a comma and a space as the separator.
To convert a comma-separated string to a list of integers:
str.split()
method to split the string on each occurrence of a
comma.int()
class to convert each string to an integer.my_str = '1,2,3,4' list_of_integers = [ int(item) if item.isdigit() else item for item in my_str.split(',') ] print(list_of_integers) # ๐๏ธ [1, 2, 3, 4]
We used the str.split
method to split the string on each occurrence of a comma
and a list comprehension
to iterate over the list.
On each iteration, we check if the current list item is a digit and use the
int()
class to convert the matching strings to integers.
The str.isdigit() method returns
True
if all characters in the string are digits and there is at least 1
character, otherwise False
is returned.
You don't have to check if you are sure that the string contains only comma-separated digits.
my_str = '1,2,3,4' my_list = [int(item) for item in my_str.split(',')] print(my_list) # ๐๏ธ [1, 2, 3, 4]
If your string contains leading or trailing commas, use a list comprehension to exclude the empty string elements from the list.
my_str = ',bobby,hadz,com,' my_list = [item for item in my_str.split(',') if item] print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com'] # --------------------------------------------------- my_str = ',1,2,3,4,' my_list = [int(item) for item in my_str.split(',') if item] print(my_list) # ๐๏ธ [[1, 2, 3, 4]
The string in the example has a leading and trailing comma, so splitting on a comma returns empty string elements.
my_str = ',bobby,hadz,com,' # ๐๏ธ ['', 'bobby', 'hadz', 'com', ''] print(my_str.split(',')) # ----------------------------------------------- my_str = ',1,2,3,4,' # ๐๏ธ ['', '1', '2', '3', '4', ''] print(my_str.split(','))
You can use a list comprehension to exclude the empty strings from the list.
You can also use the map()
function to convert a comma-separated string to a
list of integers.
my_str = '1,2,3,4' my_list = list(map(int, my_str.split(','))) print(my_list) # ๐๏ธ [1, 2, 3, 4]
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
We passed the int()
class to the map()
function, so the class will get
invoked with each string from the list.
The last step is to use the list() class to
convert the map
object to a list.
Use the str.join()
method to convert a list to a comma-separated string.
The str.join()
method will join the list's elements into a string with a comma
separator.
# โ Convert a list of strings to a comma-separated string list_of_strings = ['bobby', 'hadz', 'com'] my_str = ','.join(list_of_strings) print(my_str) # ๐๏ธ bobby,hadz,com
We used the str.join()
method to convert a list to a comma-separated string.
The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
TypeError
if there are any non-string values in the iterable.If your list contains numbers or other types, convert all of the values to
strings before calling join()
.
The following code sample converts a list of integers to a comma-separated string.
list_of_integers = [1, 3, 5, 7] my_str = ','.join(str(item) for item in list_of_integers) print(my_str) # ๐๏ธ 1,3,5,7
We used a generator expression to iterate over the list and then we used the str() class to convert each integer to a string.
The string the join()
method is called on is used as the separator between the
elements.
list_of_strings = ['bobby', 'hadz', 'com'] my_str = ','.join(list_of_strings) print(my_str) # ๐๏ธ bobby,hadz,com
If you don't need a separator and just want to join the list's elements into a
string, call the join()
method on an empty string.
list_of_strings = ['bobby', 'hadz', 'com'] my_str = ''.join(list_of_strings) print(my_str) # ๐๏ธ bobbyhadzcom
If you need to join the list's elements with a space separator, call the
join()
method on a string containing a space.
list_of_strings = ['bobby', 'hadz', 'com'] my_str = ' '.join(list_of_strings) print(my_str) # ๐๏ธ bobby hadz com
You can also use the map()
function to convert the items in the list to
strings before calling join()
.
This is a three-step process:
map()
function with the str
class and the list of integers.map()
function will convert each integer to a string.str.join()
method to join the map
object into a comma-separated
string.list_of_integers = [1, 3, 5, 7] my_str = ','.join(map(str, list_of_integers)) print(my_str) # ๐๏ธ 1,3,5,7
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
The str()
class gets called with each number in the list and converts the
values to strings.
The last step is to use the str.join()
method to join the map
object into a
comma-separated string.
You can adjust the separator if you need to.
Here is an example that joins the list of integers with a comma and a space.
list_of_integers = [1, 3, 5, 7] my_str = ', '.join(map(str, list_of_integers)) print(my_str) # ๐๏ธ 1, 3, 5, 7
The string the join()
method is called on is used as the separator between the
elements.
You can also use a list comprehension to convert a list to a comma-separated string.
list_of_integers = [1, 3, 5, 7] my_str = ','.join(str(x) for x in list_of_integers) print(my_str) # ๐๏ธ 1,3,5,7
On each iteration, we convert the current list item to a string and return the result.
The last step is to join the generator object with a comma as the separator.
I've also written an article on how to convert a comma-separated string to a dictionary.
You can learn more about the related topics by checking out the following tutorials: