Problem Formulation and Solution Overview
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
. Fergus needs your help.list
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’] |
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’] |
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] |
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] |
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’) |
Summary
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“. π