12

I take a string of integers as input and there are no spaces or any kind of separator:

12345

Now I want this string to converted into a list of individual digits

[1,2,3,4,5]

I've tried both

numlist = map(int,input().split(""))

and

numlist = map(int,input().split(""))

Both of them give me Empty Separator error. Is there any other function to perform this task?

iammrmehul
  • 730
  • 1
  • 14
  • 35

4 Answers4

21

You don't need to use split here:

>>> a = "12345"    
>>> map(int, a)
[1, 2, 3, 4, 5]

Strings are Iterable too

For python 3x:

list(map(int, a))
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • 2
    This is what I was looing for. This perfectly served my purpose. My question is marked as duplicate, but in the other question, no answer explains how to split an input as in this answer. Please see to that @Carsten. – iammrmehul Apr 02 '15 at 09:31
5

Use list_comprehension.

>>> s = "12345"
>>> [int(i) for i in s]
[1, 2, 3, 4, 5]
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1

The answers are brilliant. Here is an approach using regex

>>> import re
>>> s = '12345'
>>> re.findall(r'\d',s)
['1', '2', '3', '4', '5']

Ref - Docs on Regex

Why use regex for everything?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
-3

You also can use

>>> range(1,6)
[1, 2, 3, 4, 5]
Ishikawa Yoshi
  • 1,779
  • 8
  • 22
  • 43