9

I would like to take the following lists:

matrix1 = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]

matrix2 = [
[A, B, C, D],
[E, F, G, H]
]

and combine them into:

new_matrix = [
[A, B, C, D],
[E, F, G, H],
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]

And I can't seem to figure out a good method. Insert() puts the whole list in, resulting in a list of lists of lists. Any suggestions would be appreciated!

user2238685
  • 707
  • 1
  • 6
  • 8

5 Answers5

9

Just ADD them!

new_matrix = matrix1 + matrix2
nye17
  • 12,857
  • 11
  • 58
  • 68
3

Use + to add them:

In [59]: new_matrix = matrix2 + matrix1

In [60]: new_matrix
Out[60]: 
[['A', 'B', 'C', 'D'],
 ['E', 'F', 'G', 'H'],
 [1, 2, 3, 4],
 [5, 6, 7, 8],
 [9, 10, 11, 12]]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
2

use extend it extends the list with another rather than inserting it inside.

>>> matrix2.extend(matrix1)

However, this will make the changes in place rather than creating a new list, which might be what you want. If you would rather create a new one, then + is what you need.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Meitham
  • 9,178
  • 5
  • 34
  • 45
  • 2
    +1, It's worth noting that, while this is correct, the OP shows a new variable holding the values, while this modifies the list in-place. It obviously depends which is wanted. – Gareth Latty Apr 24 '13 at 20:49
  • @Lattyware thanks for noting that. I have updated the answer to make that clear. – Meitham Apr 24 '13 at 20:52
0

Just use the + operator

>>> a = [[1],[2],[3]]
>>> b = [[4],[5],[6]]
>>> a+b
[[1], [2], [3], [4], [5], [6]]
>>> 
Kartik
  • 9,463
  • 9
  • 48
  • 52
0

Generic solutions for many lists:

Either:

new_matrix = list(itertools.chain(matrix1, matrix2, matrix3, ...)

Or:

new_matrix = sum(matrix1, matrix2, matrix3, ..., default=[])

Or with a list of lists:

new_matrix = list(itertools.chain(*matrices)

Or:

new_matrix = sum(*matrices, default=[])
Wolph
  • 78,177
  • 11
  • 137
  • 148
  • 1
    -1, [the documentation for `sum()` specifically recommends against this usage](http://docs.python.org/3.3/library/functions.html#sum). [`itertools.chain()`](http://docs.python.org/3/library/itertools.html#itertools.chain) is the better solution. – Gareth Latty Apr 24 '13 at 20:50
  • @Lattyware: please specify what part of that is recommending against this in the case of wanting a `list` as an endresult? The OP isn't asking for an iterable, is he? – Wolph Apr 24 '13 at 20:52
  • @Lattyware: would you prefer `list(itertools.chain(*matrices))` instead? – Wolph Apr 24 '13 at 20:53
  • Yes, a list is an iterable like any other, `itertools.chain()` is the better solution for it. As to getting a list out, as you have stated, the conversion to a list is easy - if it's necessary. – Gareth Latty Apr 24 '13 at 20:53
  • @Lattyware: I repeat the question, where does it say that `itertools.chain` is recommended over `sum` if you want to make a list?` It says that there are good alternatives, that does not imply that the former is wrong... – Wolph Apr 24 '13 at 20:57
  • It may not outright state it there, but the issue is that it's a very inefficient method. Using `sum()` on a list, means making a ton of lists in-between (each partial list), this is relatively slow (remember Python's lists are not linked lists). Using `itertools.chain()` is a much more efficient solution. – Gareth Latty Apr 24 '13 at 21:03