How to convert string list to list in python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Many times, we come over the dumped data that is found in the string format and we require it to be represented into the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to list to perform tasks are quite common in web development. Let’s discuss certain ways in which this can be performed.

    Method #1: Using split() and strip()

    ini_list = "[1, 2, 3, 4, 5]"

    print ("initial string", ini_list)

    print (type(ini_list))

    res = ini_list.strip('][').split(', ')

    print ("final list", res)

    print (type(res))

    Output:

    initial string [1, 2, 3, 4, 5]
    <class 'str'>
    final list ['1', '2', '3', '4', '5']
    <class 'list'>
    

     
    Method #2: Using ast.literal_eval()

    import ast

    ini_list = "[1, 2, 3, 4, 5]"

    print ("initial string", ini_list)

    print (type(ini_list))

    res = ast.literal_eval(ini_list)

    print ("final list", res)

    print (type(res))

    Output:

    initial string [1, 2, 3, 4, 5]
    <class 'str'>
    final list [1, 2, 3, 4, 5]
    <class 'list'>
    

     
    Method #3: Using json.loads()

    import json

    ini_list = "[1, 2, 3, 4, 5]"

    print ("initial string", ini_list)

    print (type(ini_list))

    res = json.loads(ini_list)

    print ("final list", res)

    print (type(res))

    Output:

    initial string [1, 2, 3, 4, 5]
    <class 'str'>
    final list [1, 2, 3, 4, 5]
    <class 'list'>
    


    There isn't any need to import anything or to evaluate. You can do this in one line for most basic use cases, including the one given in the original question.

    One liner

    l_x = [i.strip() for i in x[1:-1].replace('"',"").split(',')]
    

    Explanation

    x = '[ "A","B","C" , " D"]'
    # String indexing to eliminate the brackets.
    # Replace, as split will otherwise retain the quotes in the returned list
    # Split to convert to a list
    l_x = x[1:-1].replace('"',"").split(',')
    

    Outputs:

    for i in range(0, len(l_x)):
        print(l_x[i])
    # vvvv output vvvvv
    '''
     A
    B
    C
      D
    '''
    print(type(l_x)) # out: class 'list'
    print(len(l_x)) # out: 4
    

    You can parse and clean up this list as needed using list comprehension.

    l_x = [i.strip() for i in l_x] # list comprehension to clean up
    for i in range(0, len(l_x)):
        print(l_x[i])
    # vvvvv output vvvvv
    '''
    A
    B
    C
    D
    '''
    

    Nested lists

    If you have nested lists, it does get a bit more annoying. Without using regex (which would simplify the replace), and assuming you want to return a flattened list (and the zen of python says flat is better than nested):

    x = '[ "A","B","C" , " D", ["E","F","G"]]'
    l_x = x[1:-1].split(',')
    l_x = [i
        .replace(']', '')
        .replace('[', '')
        .replace('"', '')
        .strip() for i in l_x
    ]
    # returns ['A', 'B', 'C', 'D', 'E', 'F', 'G']
    

    If you need to retain the nested list it gets a bit uglier, but it can still be done just with regular expressions and list comprehension:

    import re
    
    x = '[ "A","B","C" , " D", "["E","F","G"]","Z", "Y", "["H","I","J"]", "K", "L"]'
    # Clean it up so the regular expression is simpler
    x = x.replace('"', '').replace(' ', '')
    # Look ahead for the bracketed text that signifies nested list
    l_x = re.split(r',(?=\[[A-Za-z0-9\',]+\])|(?<=\]),', x[1:-1])
    print(l_x)
    # Flatten and split the non nested list items
    l_x0 = [item for items in l_x for item in items.split(',') if not '[' in items]
    # Convert the nested lists to lists
    l_x1 = [
        i[1:-1].split(',') for i in l_x if '[' in i
    ]
    # Add the two lists
    l_x = l_x0 + l_x1
    

    This last solution will work on any list stored as a string, nested or not.

    How do I convert a string to a list in Python?

    The split() method is the recommended and most common method used to convert string to list in Python.

    How do I convert a string to a list that looks like a list in Python?

    How to Convert a String to a List of Words. Another way to convert a string to a list is by using the split() Python method. The split() method splits a string into a list, where each list item is each word that makes up the string. Each word will be an individual list item.

    How do I convert a list of strings to a list of objects?

    Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);

    How do you convert a list of strings to a list of numbers?

    The most Pythonic way to convert a list of strings to a list of ints is to use the list comprehension [int(x) for x in strings] . It iterates over all elements in the list and converts each list element x to an integer value using the int(x) built-in function.