0

variable a contains a list of dictionaries but its type is of string.

a

output:"[{'id': 35, 'name': 'Comedy'}, {'id': 18, 'name': 'Drama'}, {'id': 10751, 'name': 'Family'}, {'id': 10749, 'name': 'Romance'}]"

a[0]

output:'['

I want to convert a into a list so that I can access the dictionary using index

Aptha Gowda
  • 968
  • 2
  • 10
  • 20
  • 3
    Try not to get into this situation in the first place; use the `json` module to produce serialisations of such objects instead of using `str()` on a list to store it in a file or similar. – Martijn Pieters Feb 09 '19 at 14:30

1 Answers1

2

You can use the ast package.

import ast

ast.literal_eval(your_str)
Tdaw
  • 181
  • 4