3

I want to turn the string:

avengers,ironman,spiderman,hulk

into the list:

['avengers','ironman','spiderman','hulk']

I tried

list = raw_input("Enter string:")
list = list.split()

but it's not working the right way as I need it, it's taking the whole string and makes it as one item in the list (it works fine without the "," but that's not what I need)

2
  • 1
    list.split(','). But don't use list as a variable name; you're overriding a (very useful) built-in function (called list). Commented Dec 28, 2015 at 19:52
  • @Doorknob冰 alright thanks! Commented Dec 28, 2015 at 20:02

2 Answers 2

8

If you dont pass anything to split method, it splits on empty space. Pass the comma as argument:

my_list.split(',')

edited so you dont use list as name

Sign up to request clarification or add additional context in comments.

Comments

2

Hello guys i want to make for example the next string:

avengers,ironman,spiderman,hulk into the next list:


['avengers','ironman','spiderman','hulk']

i tried that `

list = raw_input("Enter string:")
    list = list.split()

Do this instead:

list = raw_input("Enter string:")
list = list.split(",")

And, as mentioned by the others, you might want to not use the name "list" for your string/array.

Comments

Your Answer

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.