Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split string without spaces into list of integers in Python? [duplicate]

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?

like image 820
iammrmehul Avatar asked Apr 02 '15 09:04

iammrmehul


People also ask

How do I convert a string to a list without delimiter in Python?

Method#1: Using split() method If a delimiter is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.


2 Answers

Use list_comprehension.

>>> s = "12345"
>>> [int(i) for i in s]
[1, 2, 3, 4, 5]
like image 75
Avinash Raj Avatar answered Oct 07 '22 01:10

Avinash Raj


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))
like image 34
Hackaholic Avatar answered Oct 07 '22 01:10

Hackaholic