Search code examples
listfor-comprehension

Joining lists in list together in python


Stupidly basic question, but I have the following list:

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]]

How do I join the elements together so it reads:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]

Cheers!


Solution

  • import itertools
    a = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]]
    print list(itertools.chain.from_iterable(a))
    

    Hope this helps.