"10, 20, 30" to [10, 20, 30]

Discussion in 'Python' started by Daniel Austria, Nov 23, 2006.

  1. Sorry,

    how can i convert a string like "10, 20, 30" to a list [10, 20, 30]

    what i can do is:

    s = "10, 20, 30"
    tmp = '[' + s + ']'
    l = eval(tmp)

    but in my opinion this is not a nice solution


    daniel
    Daniel Austria, Nov 23, 2006
    #1
    1. Advertising

  2. Daniel Austria

    Tech Guest

    Daniel Austria a écrit :
    > Sorry,
    >
    > how can i convert a string like "10, 20, 30" to a list [10, 20, 30]
    >
    > what i can do is:
    >
    > s = "10, 20, 30"
    > tmp = '[' + s + ']'
    > l = eval(tmp)
    >
    > but in my opinion this is not a nice solution
    >
    >
    > daniel
    >


    If you're sure that there's only ints
    l = [int(item) for item in s.split(', ')]

    Yannick
    Tech, Nov 23, 2006
    #2
    1. Advertising

  3. Daniel Austria

    John Machin Guest

    Daniel Austria wrote:
    > Sorry,
    >
    > how can i convert a string like "10, 20, 30" to a list [10, 20, 30]
    >
    > what i can do is:
    >
    > s = "10, 20, 30"
    > tmp = '[' + s + ']'
    > l = eval(tmp)
    >
    > but in my opinion this is not a nice solution


    Most people share your opinion. Try this:

    | >>> strg = "10, 20, 30"
    | >>> [int(x) for x in strg.split(',')]
    | [10, 20, 30]

    Cheers,
    John
    John Machin, Nov 23, 2006
    #3
  4. On Thu, 23 Nov 2006 03:13:10 -0800, Daniel Austria wrote:

    > Sorry,
    >
    > how can i convert a string like "10, 20, 30" to a list [10, 20, 30]
    >
    > what i can do is:
    >
    > s = "10, 20, 30"
    > tmp = '[' + s + ']'
    > l = eval(tmp)
    >
    > but in my opinion this is not a nice solution



    It is a dangerous solution if your data is coming from an untrusted source.

    >>> s = "10, 20, 30"
    >>> L = [x.strip() for x in s.split(',')]
    >>> L

    ['10', '20', '30']
    >>> L = [int(x) for x in L]
    >>> L

    [10, 20, 30]

    Or, as a one liner: [int(x.strip()) for x in s.split(',')]


    --
    Steven.
    Steven D'Aprano, Nov 23, 2006
    #4
  5. Daniel Austria

    Tim Williams Guest

    On 23/11/06, Steven D'Aprano <> wrote:
    > On Thu, 23 Nov 2006 03:13:10 -0800, Daniel Austria wrote:
    >
    > > Sorry,
    > >
    > > how can i convert a string like "10, 20, 30" to a list [10, 20, 30]
    > >
    > > what i can do is:
    > >
    > > s = "10, 20, 30"
    > > tmp = '[' + s + ']'
    > > l = eval(tmp)
    > >
    > > but in my opinion this is not a nice solution

    >
    >
    > It is a dangerous solution if your data is coming from an untrusted source.
    >
    > >>> s = "10, 20, 30"
    > >>> L = [x.strip() for x in s.split(',')]
    > >>> L

    > ['10', '20', '30']
    > >>> L = [int(x) for x in L]
    > >>> L

    > [10, 20, 30]
    >
    > Or, as a one liner: [int(x.strip()) for x in s.split(',')]


    You don't need the strip()

    >>> int(' 10 ')

    10
    >>>


    :)
    Tim Williams, Nov 23, 2006
    #5
  6. Tim Williams wrote:

    >> It is a dangerous solution if your data is coming from an untrusted source.
    >>
    >> >>> s = "10, 20, 30"
    >> >>> L = [x.strip() for x in s.split(',')]
    >> >>> L

    >> ['10', '20', '30']
    >> >>> L = [int(x) for x in L]
    >> >>> L

    >> [10, 20, 30]
    >>
    >> Or, as a one liner: [int(x.strip()) for x in s.split(',')]

    >
    > You don't need the strip()
    >
    >>>> int(' 10 ')

    > 10
    >>>>


    and the use of a list comprehension is pretty silly to, given that you want
    to apply the same *function* to all items, and don't really need to look
    it up for every item:

    map(int, s.split(','))

    </F>
    Fredrik Lundh, Nov 23, 2006
    #6
  7. Daniel Austria

    Tim Williams Guest

    On 23/11/06, Fredrik Lundh <> wrote:
    > Tim Williams wrote:
    > >>>>

    >
    > and the use of a list comprehension is pretty silly to, given that you want
    > to apply the same *function* to all items, and don't really need to look
    > it up for every item:
    >
    > map(int, s.split(','))


    Haha, thanks Frederic, I wondered how long it would take for a reply
    from you :)

    "Silly" though ??

    Tim :)
    Tim Williams, Nov 23, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page