Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to a tuple

Tags:

python

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.

like image 272
Antonio Pardo Avatar asked Feb 15 '26 11:02

Antonio Pardo


1 Answers

>>> 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')
>>> 
like image 160
Manoj Govindan Avatar answered Feb 18 '26 00:02

Manoj Govindan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!