break up a value in a list to a list of individual items
Terry Reedy
tjreedy at udel.edu
Sun Nov 9 12:44:56 EST 2008
r3bol wrote:
> 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]
More information about the Python-list
mailing list