break up a value in a list to a list of individual items

R

r3bol

Hi, sorry to post this, but I've had a really hard time finding how to
do it.
Q.
How can I break up a value in a list to a list of individual items
(preferably without importing any modules)?
Like...
['12345'] (string)
to
[1, 2, 3, 4, 5] [numbers]

Thanks.
 
C

Chris Rebert

Hi, sorry to post this, but I've had a really hard time finding how to
do it.
Q.
How can I break up a value in a list to a list of individual items
(preferably without importing any modules)?
Like...
['12345'] (string)
to
[1, 2, 3, 4, 5] [numbers]

nums = [int(char) for char in '12345']

Note also that:
list("1234") == ["1", "2", "3", "4"]

And do be sure to read one of the several fine Python tutorials.

Cheers,
Chris
 
A

Arnaud Delobelle

r3bol said:
Hi, sorry to post this, but I've had a really hard time finding how to
do it.
Q.
How can I break up a value in a list to a list of individual items
(preferably without importing any modules)?
Like...
['12345'] (string)
to
[1, 2, 3, 4, 5] [numbers]

Here's one way:
[1, 2, 3, 4, 5]

HTH
 
T

Terry Reedy

r3bol said:
Hi, sorry to post this, but I've had a really hard time finding how to
do it.
Q.
How can I break up a value in a list to a list of individual items
(preferably without importing any modules)?
Like...
['12345'] (string)
to
[1, 2, 3, 4, 5] [numbers]

You did not specify what you want to happen if the original list has
more than one item. If you want to keep the other items....
>>> lst = [1, '234', 5]
>>> lst[1:2] = [int(i) for i in lst[1]] # insert slice
>>> lst
[1, 2, 3, 4, 5]
>>> lst = [1, '234', 5]
>>> lst[1] = [int(i) for i in lst[1]] # insert item
>>> lst
[1, [2, 3, 4], 5]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top