Encode.to.code
Encode.to.code

Reputation: 99

How can i split a number that is inside a list?

like [198] how can I split it to [1,9,8]?

plus I cant show my code that I did because I didn't do one am asking this out for my own benefit or knowledge.

Upvotes: 0

Views: 73

Answers (7)

Python Experts
Python Experts

Reputation: 11

Try this code.It is working. print(list(map(int,str([198][0]))))

Upvotes: 0

akhavro
akhavro

Reputation: 101

This will do the job:

>>> [int(x) for x in list(str([198][0]))]
[1, 9, 8]

Upvotes: 0

TavoGLC
TavoGLC

Reputation: 919

You can also try

toList=lambda Number: [int(val) for val in str(Number)]

Upvotes: 0

Mehrdad Pedramfar
Mehrdad Pedramfar

Reputation: 11073

Try this:

list(map(int,str([198][0])))

Upvotes: 0

Óscar López
Óscar López

Reputation: 236034

According to the comments in the question, each list has a single number on it. Here's a simple way to do the conversion, using list comprehensions:

lst = [198]
[int(x) for x in str(lst[0])]
=> [1, 9, 8]

To extend my solution for lists with more elements (not a requirement in the question, but what the heck):

lst = [198, 199, 200]
[[int(x) for x in str(y)] for y in lst]
[[1, 9, 8], [1, 9, 9], [2, 0, 0]]

Upvotes: 2

Talha Israr
Talha Israr

Reputation: 660

Try this code. I kept it as easy as i could:

new=[]
test=[198]
for i in test:
    string=str(i)
    for j in string:
        new.append(int(j))

Hope it helps :)

Upvotes: 1

BENY
BENY

Reputation: 323306

IIUC for example you have list like l=[198,128]

l=[198,128]
[[int(y) for y in list(str(x))] for x in l]
Out[520]: [[1, 9, 8], [1, 2, 8]]

Upvotes: 1

Related Questions