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

asked Aug 27, 2012 at 2:54
2

2 Answers 2

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(',')]
answered Aug 27, 2012 at 2:59
Sign up to request clarification or add additional context in comments.

1 Comment

Perhaps tack .lstrip() on the end to remove the spaces that follow the commas.
-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

answered Aug 27, 2012 at 6:22

2 Comments

This is a string that looks a bit like the string representation of a list. A list is a very different thing to a string
Agreeing w/ @gnibbler : this answer has little to do with the question, -1.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.