6

Using Python 2.7.3.1

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

This is my code:

myList = ['hello']

myList.split()
Ced
  • 1,293
  • 5
  • 23
  • 34
sp3cro
  • 65
  • 1
  • 1
  • 4

2 Answers2

6

You can simply do list(myList[0]) as below:

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

See documentation here

user3885927
  • 3,363
  • 2
  • 22
  • 42
  • Is there a way to acheive that in one line? mylist = [list('hello)[0]) just returns 'h' as in slicing. Actually Yes. alpha = [c for c in list(string.ascii_lowercase)] – sayth Jul 26 '15 at 00:59
2

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 info on list comprehensions: http://www.secnetix.de/olli/Python/list_comprehensions.hawk

Lists in python do not have a split method. split is a method of strings(str.split())

Example:

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

By default, split splits on whitespace.

Check out more info: http://www.tutorialspoint.com/python/string_split.htm:

Totem
  • 7,189
  • 5
  • 39
  • 66