4
list_string = "[1,3,4,6]" 

I tried list_string[1:-1] but it is giving 1,3,4,6. I also tried list_string.split(',') but again the brackets will be appended to first and last element i.e '[1' , '3' , '4' , '6]'

I can iterate over all the items and remove brackets from 1st and last element. But what is the best and simple way to do this?

3
  • Is this JSON perhaps? Then use json.loads(). It'll depend on other factors however if it can be treated as JSON; if there are non-numeric values for example, such as null or false or true or strings. Commented Jun 21, 2017 at 9:48
  • In other words: please provide some more context as to where this data comes from. If you used str() or repr() on Python objects, you could use ast.literal_eval(), but perhaps you should try to avoid doing that first step in the first place. Commented Jun 21, 2017 at 9:49
  • if it's not JSON, you could combine your approaches - list_string[1:-1].split(',') Commented Jun 21, 2017 at 9:49

1 Answer 1

16

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, 2, 3] 
Sign up to request clarification or add additional context in comments.

4 Comments

Please do elaborate on when to use which one. ast.literal_eval() is slower, and there are huge differences between the two approaches.
@MartijnPieters I just know that the json is optimized to parse JSON strings, whereas literal_eval uses something like a parse tree to evaluate strings. Am I right? Will link to the docs.
JSON syntax differs materially from Python syntax. That JSON and Python syntax are the same for this one example is luck, not design.
The two functions then will act entirely differently when the input consists of something more complex than a list of numbers. The OP hasn't given us anything to go by, but future visitors stumbling upon this question may be tempted to try one or the other method and not notice that they broke something when their output consists of, say, strings with unicode escape sequences.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.