Concatenation of 2d lists | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Concatenation of 2d lists

I have 2 lists of shape [355,1600] and [5,1600]. I wish to concatenate them into a new list of shape [360,1600]. How should I do it?

14th Sep 2019, 6:53 PM
Kunal Rustagi
Kunal Rustagi - avatar
7 Answers
+ 1
You should've mentioned it was for numpy arrays. Use np.vstack() to concatenate the arrays vertically (same amount of columns, different amount of rows) or np.hstack() to concatenate them horizontally (same amount of rows, different amount of columns) import numpy as np # 2 rows, 3 columns n1 = np.array([[1,2,3],[4,5,6]]) # 1 row, 3 columns n2 = np.array([[7,8,9]]) # 1+2==3 rows, 3 columns n3 = np.vstack((n1,n2)) print(n3) # [[1 2 3] # [4 5 6] # [7 8 9]]
15th Sep 2019, 4:08 AM
Diego
Diego - avatar
+ 1
if you meant the result to be [360, 3200] the you could do this: li1 = [355, 1600] li2 = [5, 1600] li3 = [li1[i] + li2[i] for i in range(len(li1))] if it's not this, then tell us what the system behind combining the lists is...
14th Sep 2019, 7:27 PM
Anton Böhler
Anton Böhler - avatar
+ 1
# 2 rows, 3 columns n1 = [[1, 2, 3], [4, 5, 6]] # 1 row, 3 columns n2 = [[7, 8, 9]] # 1+2==3 rows, 3 columns n3 = n1+n2 print(n3) # [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
15th Sep 2019, 4:00 AM
Diego
Diego - avatar
+ 1
Diego is that true for numpy arrays as well? Just asking.
15th Sep 2019, 4:01 AM
Kunal Rustagi
Kunal Rustagi - avatar
+ 1
Diego I wanted it for lists only. But was curious to know about the numpy array as well because I might use it instead of lists. Thank you! Much appreciated.
15th Sep 2019, 4:34 AM
Kunal Rustagi
Kunal Rustagi - avatar
0
Anton Böhler I have mentioned that the new list should be of shape[360,1600]. Thank you.
15th Sep 2019, 3:52 AM
Kunal Rustagi
Kunal Rustagi - avatar
0
~ swim ~ Yeah I'll try this. Sounds clear. I am not trying to perform addition on the lists, just an append like operation. n1 has 355 rows and n2 has 5 rows. Now n3 should have 360 rows. By your comment, I understand the operation of first copying n1 in n3 and then n2 in n3. Thank you.
15th Sep 2019, 3:55 AM
Kunal Rustagi
Kunal Rustagi - avatar