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

Using Python 2.7.3.1

I don't understand what is the problem with my coding! I am getting this error:AttributeError: 'list' object has no attribute 'split

This is my code:

myList = ['hello']

myList.split()

      

+3


source to share


2 answers


You can simply do list(myList[0])

as below:

>>> myList = ['hello']
>>> myList=list(myList[0])
>>> myList
['h', 'e', 'l', 'l', 'o']

      



See documentation here

+2


source


To achieve what you are looking for:

myList = ['hello']
result = [c for c in myList[0]] # a list comprehension

>>> print result
 ['h', 'e', 'l', 'l', 'o']

      

More information on lists: http://www.secnetix.de/olli/Python/list_comprehensions.hawk

Lists in python don't have a split method. split is a string ( str.split()

) method



Example:

>>> s = "Hello, please split me"
>>> print s.split()
['Hello,', 'please', 'split', 'me']

      

By default, the separation is split into spaces.

More information: http://www.tutorialspoint.com/python/string_split.htm :

+1


source







All Articles