-2

how to extract list or dictionary from String ? Convert String to List and Dictionary

I want to iterat through the list to print the dictionary value but i am not able to convert that string into list with proper format.

pro = '[{"name": "Ingenious Film Partners", "id": 289}, {"name": "Twentieth Century Fox Film Corporation", "id": 306}]'
print(type(pro))
<class 'str'>

i want list from the string but when i try to convert string into list it will return each character as list element.

pro = list(pro)
print(pro)
['[', '{', '"', 'n', 'a',........'6', '}', ']']

but i want to list with dictionary elements.

  • Does this answer your question? [Convert string representation of list to list](https://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list) – CDJB Feb 06 '20 at 15:34
  • Does this answer your question? [Convert string representation of list to list](https://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list) – NicoT Feb 06 '20 at 15:37

2 Answers2

0

That's a string in JSON format, you could use:

import json

pro = '[{"name": "Ingenious Film Partners", "id": 289}, {"name": "Twentieth Century Fox Film Corporation", "id": 306}]'
o = json.loads()

print(o[0]['name'])
Ajordat
  • 1,320
  • 2
  • 7
  • 19
0

run this:

import json
pro = '[{"name": "Ingenious Film Partners", "id": 289}, {"name": "Twentieth Century Fox Film Corporation", "id": 306}]'
print(type(pro))
jsonData=json.loads(pro)
print(type(jsonData))
print(jsonData[0]["name"])
First Last
  • 19
  • 5