Python 2: AttributeError: the 'list' object does not have the 'strip' attribute

I have a little list problem. So I have a list called l :

 l = ['Facebook;Google+;MySpace', 'Apple;Android'] 

And as you can see, I have only 2 lines in my list. I want to separate my list l from ';' and put my new 5 lines in a new list called l1 .

How can i do this?

And also I tried to do it like this:

 l1 = l.strip().split(';') 

But Python will tell me the error:

 AttributeError: 'list' object has no attribute 'strip' 

So, if the list object does not have the strip or split attribute, how can I split the list?

thanks

+7
python list split
source share
7 answers

strip() is a method for strings, you call it on list , hence the error.

 >>> 'strip' in dir(str) True >>> 'strip' in dir(list) False 

To do what you want, just do

 >>> l = ['Facebook;Google+;MySpace', 'Apple;Android'] >>> l1 = [elem.strip().split(';') for elem in l] >>> print l1 [['Facebook', 'Google+', 'MySpace'], ['Apple', 'Android']] 

Since you want the items to be on the same list (rather than a list of lists), you have two options.

  • Create an empty list and add items to it.
  • Smooth the list.

To do the first, execute the code:

 >>> l1 = [] >>> for elem in l: l1.extend(elem.strip().split(';')) >>> l1 ['Facebook', 'Google+', 'MySpace', 'Apple', 'Android'] 

To do the second, use itertools.chain

 >>> l1 = [elem.strip().split(';') for elem in l] >>> print l1 [['Facebook', 'Google+', 'MySpace'], ['Apple', 'Android']] >>> from itertools import chain >>> list(chain(*l1)) ['Facebook', 'Google+', 'MySpace', 'Apple', 'Android'] 
+14
source share

This should be what you want:

 [x for y in l for x in y.split(";")] 

exit:

 ['Facebook', 'Google+', 'MySpace', 'Apple', 'Android'] 
+2
source share

Hope this helps :)

 >>> x = [i.split(";") for i in l] >>> x [['Facebook', 'Google+', 'MySpace'], ['Apple', 'Android']] >>> z = [j for i in x for j in i] >>> z ['Facebook', 'Google+', 'MySpace', 'Apple', 'Android'] >>> 
+2
source share

What do you want to do -

 strtemp = ";".join(l) 

The first line adds ; to the end of MySpace , so when splitting it does not MySpaceApple This will combine l into one line, and then you can simply

 l1 = strtemp.split(";") 

This works because strtemp is a string that has .split ()

+1
source share

One of the possible solutions that I tried right now: (Make sure you do this in the general case, using while with the index)

 >>> l=['Facebook;Google+;MySpace', 'Apple;Android'] >>> new1 = l[0].split(';') >>> new1 ['Facebook', 'Google+', 'MySpace'] >>> new2= l[1].split(';')`enter code here` >>> new2 ['Apple', 'Android'] >>> totalnew = new1 + new2 >>> totalnew ['Facebook', 'Google+', 'MySpace', 'Apple', 'Android'] 
+1
source share

You have split the line in the list. l [0] .strip ()

0
source share

Separate the strings and then use chain.from_iterable to combine them into one list

 >>> import itertools >>> l = ['Facebook;Google+;MySpace', 'Apple;Android'] >>> l1 = [ x for x in itertools.chain.from_iterable( x.split(';') for x in l ) ] >>> l1 ['Facebook', 'Google+', 'MySpace', 'Apple', 'Android'] 
0
source share

All Articles