Question: I know that to convert a dictionary to/from a string, I use `json.loads` and `json.dumps`. However, those methods fail when given a string representing a list of dictionaries. For example, 

```
sample_entry = [
    {"type": "test", "topic": "obama", "interval": "daily"},
    {"type": "test", "topic": "biden", "interval": "immediate"},
]

```

Converting that to a string with `str()` gives us 

```
"[{'topic': 'obama', 'interval': 'daily', 'type': 'test'}, {'topic': 'biden', 'interval': 'immediate', 'type': 'test'}]"

```

Is there an easy way of converting this back to a list of dictionaries? `eval` does precisely what I want, but I'm concerned about running it on a user-supplied string. 

Is there another way of accomplishing this that will be robust in the face of varying list lengths and variable white space?

EDIT: While json.loads(json.dumps(sample\_entry)) does indeed work, json.loads(str(sample\_entry)) does not. The difference, it seems, is where single quotes and double quotes are used. The data source I'm receiving this string from is unlikely to be consistent in use of ' vs ", so I'd like to account for both.

Answer: 
You may use [`ast.literal_eval`](https://docs.python.org/2/library/ast.html#ast.literal_eval):

```
>>> import ast
>>> my_str = "[{'topic': 'obama', 'interval': 'daily', 'type': 'test'}, {'topic': 'biden', 'interval': 'immediate', 'type': 'test'}]"

>>> ast.literal_eval(my_str)
[{'interval': 'daily', 'type': 'test', 'topic': 'obama'}, {'interval': 'immediate', 'type': 'test', 'topic': 'biden'}]

```

As per the [`ast.literal_eval` document](https://docs.python.org/2/library/ast.html#ast.literal_eval), it:

> 
> Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.
> 
> 
>
A well-formed python data type can in most case be interpreted using [`ast.literal_eval`](https://docs.python.org/2/library/ast.html#ast.literal_eval)

```
import ast
ast.literal_eval("[{'topic': 'obama', 'interval': 'daily', 'type': 'test'}, {'topic': 'biden', 'interval': 'immediate', 'type': 'test'}]")
Out[38]: 
[{'interval': 'daily', 'topic': 'obama', 'type': 'test'},
 {'interval': 'immediate', 'topic': 'biden', 'type': 'test'}]

```