How to Create a List from a Comma-Separated String

5/5 - (1 vote)

Problem Formulation and Solution Overview

In this article, you’ll learn how to convert a comma-separated string into a List.

Fergus, a 10-year-old boy, is learning to code with Python. As homework, the teacher has asked the class to create a comma-separated string and convert this string into a list. Fergus needs your help.


πŸ’¬ Question: How would we write Python code to convert a comma-separated string into a list?

We can accomplish this task by one of the following options:


Method 1: Use split()

This method uses split() to break the String into elements based on the separator. Then split() returns a list containing these same elements.

pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper'
pet_list = pet_names.split(',')
print(pet_list)

Above, creates a comma-separated string containing six (6) pet names and saves these to pet_names.

Next, split() is appended to pet_names and passed a separator ((',')). This indicates to split() to break the string pet_names into smaller chunks (elements) at the said character (',').

This results in a list containing six (6) string elements that are output to the terminal.

[‘Chewie’, ‘Peppy’, ‘Elvis’, ‘Axel’, ‘Banjo’, ‘Draper’]
Python String Methods [Ultimate Guide]

Method 2: Use List Comprehension and split()

This method uses List Comprehension in conjunction with split() to convert a comma-separated string into a list.

pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper'
pet_list = [x for x in pet_names.split(',')]
print(pet_list)

Above, creates a comma-separated string containing six (6) pet names and saves these to pet_names.

Next, List Comprehension is used to loop through pet_names and break the string into smaller chunks (elements) at the said character (','). This results in a list containing six (6) string elements.

The contents of pet_list are output to the terminal.

[‘Chewie’, ‘Peppy’, ‘Elvis’, ‘Axel’, ‘Banjo’, ‘Draper’]
Python One-Liner Trick 9 - Nested List Comprehension

Method 3: Use List Comprehension and findall()

This method imports the regex library and calls the findall() function to locate all occurrences of a specified character and split accordingly.

import re
pet_ages = "12, 4, 8, 2, 1, 7"
pet_list = [int(i) for i in re.findall(r'\d+', pet_ages)]
print(pet_list)

Above, imports the regex library. Click here if this requires installation.

Next, we create a comma-separated string containing six (6) pet ages and save these to pet_ages.

Using List Comprehension, we loop through pet_ages, finding all occurrences of one or more digits ('\d+‘), split the string appropriately, convert to integers and save it to pet_list. This results in a list containing six (6) integer elements.

The contents of pet_list are output to the terminal.

[12, 4, 8, 2, 1, 7]
5 must have skills to become a programmer (that you didn't know)

Method 4: Use List Comprehension and map()

This method uses List Comprehension in conjunction with split() and map() to convert a comma-separated string into a list.

pet_ages = "12, 4, 8, 2, 1, 7"
pet_list = [i for i in map(int, pet_ages.split(','))]
print(pet_list)

Above, we create a comma-separated string containing six (6) pet ages and save these to pet_ages.

The map() function is used and accepts two (2) arguments: a data type (int), and an iterable (pet_ages.split(',')). This is then converted to a map() object similar to below:

<map object at 0x0000018D8AC6AA70>

Next, using List Comprehension, we convert this object to a list of integers and output it to the terminal.

[12, 4, 8, 2, 1, 7]
Mastering the Python Map Function [+Video]

Bonus: Convert to a Tuple

In some instances, you may want to convert a comma-separated string into a tuple.

pet_names = 'Chewie, Peppy, Elvis, Axel, Banjo, Draper'
pet_tuple = tuple(pet_names.split(','))
print(pet_tuple)

Above, creates a comma-separated string containing six (6) pet names and saves these to pet_names.

Next, tuple is called and passed an argument (pet_names.split(',')). This statement splits the string into smaller chunks (elements) based on the separator.

This results in a tuple containing six (6) string elements that are output to the terminal.

(‘Chewie’, ‘ Peppy’, ‘ Elvis’, ‘ Axel’, ‘ Banjo’, ‘ Draper’)
The Ultimate Guide to Python Tuples | Intro and Theoretical Basics | Part 1/7

Summary

These methods of converting a comma-separated string into a list should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!


Programmer Humor

πŸ‘±β€β™€οΈ Programmer 1: We have a problem
πŸ§”β€β™‚οΈ Programmer 2: Let’s use RegEx!
πŸ‘±β€β™€οΈ Programmer 1: Now we have two problems

… yet – you can easily reduce the two problems to zero as you polish your “RegEx Superpower in Python“. πŸ™‚