-2

Hello I have a list containing one string that I need to split in two on the comma:

value = ['Red, Orange']

I split using :

value = value[0].split(", ")

But I get :

value[0] = "Red"

and

value[1:] = ["Orange"]

why is value[1:] a list? What can I do to get

value[1:] = "Orange"

Thanks

salamey
  • 3,633
  • 10
  • 38
  • 71

2 Answers2

0

You simply do:

value[1]

without the :. This indicates to python that you are getting an element at a specific index and not a portion of the string from index 1 to the last element.

value[0] will contain "Red"

and

value[1] will contain "Orange"

What you are doing with the [:] is known as list slicing. Perhaps you should read about it so that it is clear.

sshashank124
  • 31,495
  • 9
  • 67
  • 76
-1

You may write this:

value = [x for x in value[0].split(", ")]

It's called list comprehension and it will work for arbitrary number of words in value[0]

evocatus
  • 111
  • 1
  • 2
  • 8
  • -1, a comprehension is totally unnecessary since split already gives a list. Also this doesn't answer the question – erlc Jul 01 '14 at 09:01