I assume that you want flatten matrix (list of lists) into list.
1. Simple nested for-loop with append:
1. Simple nested for-loop with append:
>>> m = [ ... [1, 2, 3], ... [4, 5, 6], ... [7, 8, 9], ... ] >>> f = [] >>> for x in m: ... for y in x: ... f.append(y) ... >>> f [1, 2, 3, 4, 5 ,6 ,7 ,8, 9]2. List comprehension
>>> m = [ ... [1, 2, 3], ... [4, 5, 6], ... [7, 8, 9], ... ] >>> [y for x in m for y in x] [1, 2, 3, 4, 5 ,6 ,7 ,8, 9]3. Using sum
>>> m = [ ... [1, 2, 3], ... [4, 5, 6], ... [7, 8, 9], ... ] >>> sum(m, []) [1, 2, 3, 4, 5 ,6 ,7 ,8, 9]4. Using itertools
>>> import itertools >>> m = [ ... [1, 2, 3], ... [4, 5, 6], ... [7, 8, 9], ... ] >>> list(itertools.chain(*m)) [1, 2, 3, 4, 5 ,6 ,7 ,8, 9]etc, etc, etc
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.