-1

I have a string array that looks like this:

string = [ [ "this is a sample",
             "this is another sample"],
           [ "The third sample",
             "the fourth one"] ]

But I want to convert it to:

string = [   "this is a sample",
             "this is another sample",
             "The third sample",
             "the fourth one"  ]

How can I do it? I know I can do it by pre-allocating a string and iterating. But is there a simpler method?

ponir
  • 447
  • 7
  • 20
  • 1
    Google "Python flatten list of lists". Also refer: http://stackoverflow.com/questions/11264684/flatten-list-of-lists – sisanared Oct 21 '16 at 04:44

2 Answers2

1

You can try like this using list comprehension. Code:

string = [ [ "this is a sample",
         "this is another sample"],
       [ "The third sample",
         "the fourth one"] ]

print([_ for i in range(len(string)) for _ in string[i]])
Md Mahfuzur Rahman
  • 2,319
  • 2
  • 18
  • 28
1

Maybe use list comprehension. Something like

string = [s for S in string for s in S]
Tammo Heeren
  • 1,966
  • 3
  • 15
  • 20