Home > front end >  How do you separate every element from a list
How do you separate every element from a list

Time:01-23

l1 = [2345]

I want the following output

[2, 3, 4, 5]

CodePudding user response:

use this block of code:

l1 = [2345]
l1_str = str(l1[0])
l1_sep = []
for i in l1_str:
    l1_sep.append(int(i))
print(l1_sep)

or something like this:

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

CodePudding user response:

If you always have a single-element list then you could do this:

li1 = [1234]
li_output = [int(elem) for elem in str(li1[0])]

print(li_output)

>>> [1, 2, 3, 4]

CodePudding user response:

or you can use a construct like this

l1 = [2345]
s1 = str(l1[0])
l2 = [number for number in s1 ]
print(l2)
  •  Tags:  
  • Related