Convert a comma-separated String to a List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
6 min

banner

# Table of Contents

  1. Convert a comma-separated string to a List in Python
  2. Convert a comma-separated string to a List of Integers in Python
  3. Convert a List to a comma-separated String in Python
  4. Convert a List of integers to a comma-separated String in Python

# Convert a comma-separated string to a List in Python

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.

main.py
my_str = 'bobby,hadz,com' my_list = my_str.split(',') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

convert comma separated string to list

The code for this article is available on GitHub

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:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)
When no separator is passed to the 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.

main.py
print('one'.split(',')) # ๐Ÿ‘‰๏ธ ['one']

Make sure to adjust the separator if your string has a different delimiter.

main.py
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.

# Convert a comma-separated string to a List of Integers in Python

To convert a comma-separated string to a list of integers:

  1. Use the str.split() method to split the string on each occurrence of a comma.
  2. Use a list comprehension to iterate over the list of strings.
  3. Use the int() class to convert each string to an integer.
main.py
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]

convert comma separated string to list of integers

The code for this article is available on GitHub

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.

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 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.

main.py
my_str = '1,2,3,4' my_list = [int(item) for item in my_str.split(',')] print(my_list) # ๐Ÿ‘‰๏ธ [1, 2, 3, 4]

# Handling strings that contain leading or trailing comma

If your string contains leading or trailing commas, use a list comprehension to exclude the empty string elements from the list.

main.py
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]

handling strings that contain leading or trailing comma

The code for this article is available on GitHub

The string in the example has a leading and trailing comma, so splitting on a comma returns empty string elements.

main.py
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.

# Convert a comma-separated string to a List of Integers using map()

You can also use the map() function to convert a comma-separated string to a list of integers.

main.py
my_str = '1,2,3,4' my_list = list(map(int, my_str.split(','))) print(my_list) # ๐Ÿ‘‰๏ธ [1, 2, 3, 4]

convert comma separated string to list of integers using map

The code for this article is available on GitHub

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.

# Convert a List to a comma-separated String in Python

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.

main.py
# โœ… 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

convert list to comma separated string

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.

Note that the method raises a 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.

main.py
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.

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

The string the join() method is called on is used as the separator between the elements.

main.py
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.

main.py
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.

main.py
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().

# Convert a List of Integers to a comma-separated String in Python

This is a three-step process:

  1. Call the map() function with the str class and the list of integers.
  2. The map() function will convert each integer to a string.
  3. Use the str.join() method to join the map object into a comma-separated string.
main.py
list_of_integers = [1, 3, 5, 7] my_str = ','.join(map(str, list_of_integers)) print(my_str) # ๐Ÿ‘‰๏ธ 1,3,5,7
The code for this article is available on GitHub

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.

main.py
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.

# Convert a List of Integers to a comma-separated String using a generator expression

You can also use a list comprehension to convert a list to a comma-separated string.

main.py
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
The code for this article is available on GitHub
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev