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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • r3bol

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

    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.
  • Linas Juskevicius

    #2
    Re: break up a value in a list to a list of individual items

    [int(i) for i in ['12345'][0]]

    Comment

    • Chris Rebert

      #3
      Re: break up a value in a list to a list of individual items

      On Sun, Nov 9, 2008 at 2:38 AM, r3bol <leon.san.email @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...

      Comment

      • Arnaud Delobelle

        #4
        Re: break up a value in a list to a list of individual items

        r3bol <leon.san.email @gmail.comwrite s:
        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

        Comment

        • r3bol

          #5
          Re: break up a value in a list to a list of individual items

          thanks :) and so many different ways!

          Comment

          • Terry Reedy

            #6
            Re: break up a value in a list to a list of individual items

            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]

            Comment

            Working...