By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,519 Members | 1,831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,519 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Nov 9 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
[int(i) for i in ['12345'][0]]
Nov 9 '08 #2

P: n/a
On Sun, Nov 9, 2008 at 2:38 AM, r3bol <le************@gmail.comwrote:
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
--
Follow the path of the Iguana...
http://rebertia.com
>
Thanks.
--
http://mail.python.org/mailman/listinfo/python-list
Nov 9 '08 #3

P: n/a
r3bol <le************@gmail.comwrites:
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:
>>map(int, '12345')
[1, 2, 3, 4, 5]

HTH

--
Arnaud
Nov 9 '08 #4

P: n/a
thanks :) and so many different ways!
Nov 9 '08 #5

P: n/a
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]

Nov 9 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.