2

Let's say you had a string

test = 'wow, hello, how, are, you, doing'

and you wanted

full_list = ['wow','hello','how','are','you','doing']

i know you would start out with an empty list:

empty_list = []

and would create a for loop to append the items into a list

i'm just confused on how to go about this,

I was trying something along the lines of:

for i in test:
    if i == ',':

then I get stuck . . .

O.rka
  • 29,847
  • 68
  • 194
  • 309

2 Answers2

6

In Python, the nicest way to do what you want is

full_list = test.split(', ')

If your string might have some commas that aren't followed by spaces, you would need to do something a little more robust. Maybe

full_list = [x.lstrip() for x in test.split(',')]
David
  • 1,429
  • 8
  • 8
-1
>>> test = 'wow, hello, how, are, you, doing'
>>> full_list = test.replace(",","','")
>>> print full_list
wow',' hello',' how',' are',' you',' doing

i just added the flanking quotations manually

O.rka
  • 29,847
  • 68
  • 194
  • 309