string to list when the contents is a list

Discussion in 'Python' started by Wes James, Feb 17, 2010.

  1. Wes James

    Wes James Guest

    I have been trying to create a list form a string. The string will be
    a list (this is the contents will look like a list). i.e. "[]" or
    "['a','b']"

    The "[]" is simple since I can just check if value == "[]" then return []

    But with "['a','b']" I have tried and get:

    a="['a','b']"

    b=a[1:-1].split(',')

    returns

    [ " 'a' "," 'b' " ]

    when I want it to return ['a','b'].

    How can I do this?

    thx,

    -wes
    Wes James, Feb 17, 2010
    #1
    1. Advertising

  2. Wes James

    nn Guest

    Wes James wrote:
    > I have been trying to create a list form a string. The string will be
    > a list (this is the contents will look like a list). i.e. "[]" or
    > "['a','b']"
    >
    > The "[]" is simple since I can just check if value == "[]" then return []
    >
    > But with "['a','b']" I have tried and get:
    >
    > a="['a','b']"
    >
    > b=a[1:-1].split(',')
    >
    > returns
    >
    > [ " 'a' "," 'b' " ]
    >
    > when I want it to return ['a','b'].
    >
    > How can I do this?
    >
    > thx,
    >
    > -wes



    I am surprised nobody gave you the simple answer yet that may even
    work for your situation:

    b=a[2:-2].split("','")
    nn, Feb 18, 2010
    #2
    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.
Similar Threads
  1. Don Adams
    Replies:
    1
    Views:
    582
    Martin Honnen
    Mar 5, 2004
  2. feraso
    Replies:
    0
    Views:
    474
    feraso
    Apr 12, 2009
  3. Angus
    Replies:
    6
    Views:
    1,616
    James Kanze
    Jan 23, 2010
  4. Kamarulnizam Rahim
    Replies:
    4
    Views:
    205
    Robert Klemme
    Jan 28, 2011
  5. Replies:
    0
    Views:
    179
Loading...

Share This Page