62

What is a good way to do some_string.split('') in python? This syntax gives an error:

a = '1111'
a.split('')

ValueError: empty separator

I would like to obtain:

['1', '1', '1', '1']
11
  • 5
    Do you really need to do that? Most of the time you can treat a string in the same way you treat a list anyway. Commented Jun 29, 2013 at 13:16
  • 3
    I tried to do that and it gave this error, so I thought it would be nice to include here as a question Commented Jun 29, 2013 at 13:19
  • 1
    You might also find this interesting - Alternative way to split a list into groups of n. Commented Jun 29, 2013 at 13:36
  • @sqpc: No, it would not give "this error", so you didn't try that. Did you try to just do nothing? Not split it at all? Commented Jun 29, 2013 at 13:54
  • 2
    @arshajii contrary to your "most of the time ",'str' object does not support item assignment. This fill the need. Commented Apr 30, 2018 at 16:32

6 Answers 6

92

Use list():

>>> list('1111')
['1', '1', '1', '1']

Alternatively, you can use map() (Python 2.7 only):

>>> map(None, '1111')
['1', '1', '1', '1']

Time differences:

$ python -m timeit "list('1111')"
1000000 loops, best of 3: 0.483 usec per loop
$ python -m timeit "map(None, '1111')"
1000000 loops, best of 3: 0.431 usec per loop
Sign up to request clarification or add additional context in comments.

Comments

13

One can cast strings to list directly

>>> list('1111')
['1', '1', '1', '1']

or using list comprehensions

>>> [i for i in '1111']
['1', '1', '1', '1']

second way can be useful if one wants to split strings for substrings more than 1 symbol length

>>> some_string = '12345'
>>> [some_string[i:i+2] for i in range(0, len(some_string), 2)]
['12', '34', '5']

Comments

5

Strings are iterables and can be indexed, hence you don't really need to split it at all:

>>> for char in '11111':
...   print char
... 
1
1
1
1
1
>>> '11111'[4]
'1'

You can "split" it with a call to list, but it doesn't make much difference:

>>> for char in list('11111'):
...   print char
... 
1
1
1
1
1
>>> list('11111')[4]
'1'

So you only need to do this if your code explicitly expects a list. For example:

>>> list('11111').append('2')
>>> l = list('11111')
>>> l.append(2)
>>> l
['1', '1', '1', '1', '1', 2]

This doesn't work with a straight string:

>>> l.append('2')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'

In that case you would need:

>>> l += '2'
>>> l
'111112'

Comments

3

Method #1:

s="Amalraj"
l=[i for i in s]
print(l)

Output:

['A', 'm', 'a', 'l', 'r', 'a', 'j']

Method #2:

s="Amalraj"
l=list(s)
print(l)

Output:

['A', 'm', 'a', 'l', 'r', 'a', 'j']

Method #3:

import re; # importing regular expression module
s="Amalraj"
l=re.findall('.',s)
print(l)

Output:

['A', 'm', 'a', 'l', 'r', 'a', 'j']

Comments

2

If like me you arrived here to split in packets and not char by char, I made this :

>>> n=2
>>> str='423346396677674A446E6838436E304164334A30427874784467567863334942446E41424E413D3D'
>>> [ str[byte:byte+n] for byte in range(0,len(str)) if byte % n == 0 ]
['42', '33', '46', '39', '66', '77', '67', '4A', '44', '6E', '68', '38', '43', '6E', '30', '41', '64', '33', '4A', '30', '42', '78', '74', '78', '44', '67', '56', '78', '63', '33', '49', '42', '44', '6E', '41', '42', '4E', '41', '3D', '3D']

Comments

1

You can even omit the list() call altogether, with a combination of map + lambda fn and then use unpacking.

>>> s = "efgvdfbdb"
>>> [* map(lambda x: x, s) ]
['e', 'f', 'g', 'v', 'd', 'f', 'b', 'd', 'b']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.