How to Convert String Representation of List to a List

Converting a string representation of a list into an actual list object

>>> fruits = "['apple', 'orange', 'banana']"
>>> import ast
>>> fruits = ast.literal_eval(fruits)
>>> fruits
['apple', 'orange', 'banana']
>>> fruits[1]
'orange'

As pointed out in the comments ast.literal_eval is safe. From the docs:

Safely evaluate an expression node or a string containing a Python
expression. The string or node provided may only consist of the
following Python literal structures: strings, numbers, tuples, lists,
dicts, booleans, and None.

This can be used for safely evaluating strings containing Python
expressions from untrusted sources without the need to parse the
values oneself.

How to convert string representation of list to a list

>>> import ast
>>> x = '[ "A","B","C" , " D"]'
>>> x = ast.literal_eval(x)
>>> x
['A', 'B', 'C', ' D']
>>> x = [n.strip() for n in x]
>>> x
['A', 'B', 'C', 'D']

ast.literal_eval:

With ast.literal_eval you can safely evaluate an expression node or a string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, booleans, and None.

convert a string representation of a list of lists to a list in python

Use ast.literal_eval():

>>> import ast
>>> ast.literal_eval(k)
['A', ['B', 'C'], 'D']

Convert string representation of a list to a list in Java

So how to convert string [a, b] to a list of strings?

It is not intended for this purpose.

Connected with it is the question why produced string isn't ["a","b"]? Because from string representations we cannot distinguish Arrays.asList("1","2","3") from Arrays.asList(1,2,3).

This has probably to tdo with the following: Arrays.toString() just concatenates the string representations of the items with , separator and puts [] around it. So the real answer is "because string "1" and integer 1 have the same representation". And this, again, has to do with the fact that both should be printable as they are. E. g., Python differentiates between __str__() and __repr()__, Java has only .toString().

But as the .toString() outputs are (apart from certain special cases) only made for debugging, it should not matter.

And also it's not possible to parse produced string with e.g. Jackson with objectMapper.readValue(li1.toString(), String[].class);, which would be that one line solution.

If you want to do that, there are other solutions which are far better suitable for sertializtion and deserialization.

Formats for serialization are

  • binary
  • XML
  • JSON

all of which are made of a pair of unambiguous serialized data creation and deserialization which does the opposite.

How to convert string representation list with mixed values to a list?

You can first use a regex to add quotes around the potential strings (here I used letters + underscore), then use literal_eval (for some reason I have an error with pd.eval)

from ast import literal_eval
df['col_1'].str.replace(r'([a-zA-Z_]+)', r'"\1"', regex=True).apply(literal_eval)

output (lists):

0     [2, A]
1 [5, BC]

Convert string representation of list into list

Assuming your string is a simple list/dict/combination of lists and dicts, you can use one of the two packages mentioned below, listed in order of preference.

json

>>> import json
>>> json.loads('[1, 2, 3]')
[1, 2, 3]

This is definitely the preferred method if you are parsing raw json strings.



ast.literal_eval

>>> import ast
>>> ast.literal_eval('[1, 2, 3]')
[1, 2, 3]

ast.literal_eval will safely evaluate an expression node or a string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, None, bytes and sets.

Also json.loads is significantly faster than ast.literal_eval. Use this ONLY IF you cannot use the json module first.



yaml

In some cases, you can also use the pyYAML library (you'll need to use PyPi to install it first).

>>> import yaml
>>> yaml.safe_load('[1, 2, 3]')
[1, 3, 4, 6]


Related Topics



Leave a reply



Submit