I was writing custom template tags for one of my Django package. I came across a situation where I had lists in string format. I need to convert them into lists.
There are two methods to get this job done.
These two functions come in handy whenever you want to convert a list of python objects to their correct types. For example, if you have list of python objects like this
You can convert them to their corresponding types using these functions.
They yield a objects of corresponding types
References: Python Docs, Stackoverflow
There are two methods to get this job done.
1. ast.literal_eval:
This can be used to evaluate strings containing Python values from untrusted sources without parsing values.
2. json.loads:
This is used to deserialize a string to python object using a conversion table.
You can convert them to their corresponding types using these functions.
They yield a objects of corresponding types
References: Python Docs, Stackoverflow