0

I am trying remove brackets from the nested array. Thank you for helping me in advance..

this is input data

{
"th":[ [ "aa"],["bb"]]
}

expected output is

{
"th":["aa","bb"]
}

baduker
  • 19,152
  • 9
  • 33
  • 56
Vas
  • 395
  • 1
  • 3
  • 13
  • 1
    Do the answers to this [question](https://stackoverflow.com/questions/952914/how-do-i-make-a-flat-list-out-of-a-list-of-lists) help at all? – quamrana Jul 14 '22 at 11:05

2 Answers2

0

Maybe not elegant but simple:

data={
    "th":[ [ "aa"],["bb"]]
     }
data["th_mod"]=[element[0] for element in data["th"]]
data

Gives

{'th': [['aa'], ['bb']], 'th_mod': ['aa', 'bb']}

Jacob
  • 304
  • 1
  • 6
0

you can use itertools.chain.from_iterable to accomplish this:

import itertools
data["th_mod"] = itertools.chain.from_iterable(data["th"])

this can be done also using itertools.chain equivalent to this code:

import itertools
data["th_mod"] = itertools.chain(*data["th"])

here is the output:

>>> data
{"th":[["aa"],["bb"]],"th_mod":["aa","bb"]}
XxJames07-
  • 1,833
  • 1
  • 4
  • 17