3

I have a string like this:

'|Action and Adventure|Drama|Science-Fiction|Fantasy|'

How can I convert it to a tuple or a list?

Thanks.

6 Answers 6

8
>>> s = '|Action and Adventure|Drama|Science-Fiction|Fantasy|'
>>> 
>>> [item for item in s.split('|') if item.strip()]
['Action and Adventure', 'Drama', 'Science-Fiction', 'Fantasy']
>>> 

If you'd rather have a tuple then:

>>> tuple(item for item in s.split('|') if item.strip())
('Action and Adventure', 'Drama', 'Science-Fiction', 'Fantasy')
>>> 
Sign up to request clarification or add additional context in comments.

13 Comments

Also note that you should (probably) not use tuples in your case because you want multiple single component items, not one item with variable number of components.
@Alin: Didn't understand that. Care to explain?
I wanted to point out that it is not semantically correct to use tuples when you actually need lists.
Not semantically correct? Not sure why you say that, for two reasons (1) The OP specifically asked for a tuple or a list. (2) unless you are planning to modify the sequence later, a tuple would do just as fine as a list.
Alin Purcaru's argument is the one supported by Guido. Tuples are like records. Lists are sequences of objects of the same type.
|
1

You want str.split():

>>> s = '|Action and Adventure|Drama|Science-Fiction|Fantasy|'
>>> s.split('|')
['', 'Action and Adventure', 'Drama', 'Science-Fiction', 'Fantasy', '']

Comments

1

If you want to just split your string at the | character you use:

myStr.split('|')

If you also want all zero-length element removed (like the ones from the ends) you:

def myFilter(el): return len(el) > 0
filter(myFilter, myStr.split('|'))

2 Comments

Thanks won't work here ;-), Set this answer as your accepted answer" If so :)
[ x for x in myStr.split('|') if len(x) > 0 ] is much cleaner.
1

Strip 'string'.strip('|')

   >>> heading = '|Action and Adventure|Drama|Science-Fiction|Fantasy|'
   >>> tuple(heading.strip('|').split('|'))
   ('Action and Adventure', 'Drama', 'Science-Fiction', 'Fantasy')

Slice 'string'[1:-1]

   >>> heading = '|Action and Adventure|Drama|Science-Fiction|Fantasy|'
   >>> tuple(heading[1:-1].split('|'))
   ('Action and Adventure', 'Drama', 'Science-Fiction', 'Fantasy')

For List remove the tuple() call.

Comments

1

strip() gets rid of the leading and trailing chars, split() divvies up the remainder:

>>> s.strip('|').split('|')
['Action and Adventure', 'Drama', 'Science-Fiction', 'Fantasy']

Comments

0

List

seq = '|Action and Adventure|Drama|Science-Fiction|Fantasy|'.split('|')

Tuple

seq = tuple(seq)

If you want to strip empty items, pass the output through filter(None, seq). If you assume outer | always, just slice with seq[1:-1].

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.