0

I have a list of lists in python, just like such:

a = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]

And I want to create a list comprehension that will output it's elements in a list, as such:

[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

I tried doing [c for c in b for b in a] but it is now working. What am I screwing up? : )

Pablo
  • 10,425
  • 1
  • 44
  • 67

1 Answers1

0

This can be used for that:

import itertools
print(list(itertools.chain(*a)))
% gives: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

or simply:

print([v for sublist in a for v in sublist])
% gives: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
Marcin
  • 215,873
  • 14
  • 235
  • 294