I have a small problem with list. So i have a list called l
:
l = ['Facebook;Google+;MySpace', 'Apple;Android']
And as you can see I have only 2 strings in my list. I want to separate my list l
by ';' and put my new 5 strings into a new list called l1
.
How can I do that?
And also I have tried to do this like this:
l1 = l.strip().split(';')
But Python give me an error:
AttributeError: 'list' object has no attribute 'strip'
So if 'list' object has no attribute 'strip' or 'split', how can I split a list?
Thanks
This should be what you want:
output:
Split the strings and then use chain.from_iterable to combine them into a single list
What you want to do is -
The first line adds a
;
to the end ofMySpace
so that while splitting, it does not give outMySpaceApple
This will join l into one string and then you can just-This works because strtemp is a string which has .split()
Hope this helps :)
One possible solution I have tried right now is: (Make sure do it in general way using for, while with index)
You can first concatenate the strings in the list with the separator ';' using the function
join
and then use thesplit
function in order create the list:outputs