-4

So I have an API which returns a list as a string

words = "[[{'aaa':'123', 'bbb':'456',.....}, {'ccc':'123', 'ddd':'456',.....}, {'eee':'123', 'fff':'456',.....}]]"

I need to covert to readable data and remove the nested list

word = [{'aaa':'123', 'bbb':'456',.....}, {'ccc':'123', 'ddd':'456',.....}, {'eee':'123', 'fff':'456',.....}]

How do I do it in python?

  • does [this](https://stackoverflow.com/q/952914/758174) answer your problem? – Pierre D Dec 28 '20 at 14:15
  • 4
    Are you really asking how to access the first element of a list? Have you made any effort to look for an answer anywhere? What did you find and why didn't that work? – Grismar Dec 28 '20 at 14:15
  • 2
    Does this answer your question? [How to make a flat list out of list of lists?](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists) – Pierre D Dec 28 '20 at 14:15

3 Answers3

1

As the nested list is just the first item of the superior list you can just pull the first item of that superior list.

word = words[0]
umgefahren
  • 145
  • 1
  • 2
  • 11
0

you can use list comprehension for that:

words = [[a], [b], [c], [d]]
flattened = [val for sublist in words for val in sublist]
E. Gertz
  • 241
  • 4
  • 13
0

Try:

import ast
words = ast.literal_eval(words)[0]
Pygirl
  • 12,969
  • 5
  • 30
  • 43
  • I found that the issue was that it thought that `word` was a string not a list so @umgefahren answer did not work. Your solution fixed everything, thanks. –  Dec 28 '20 at 14:58