Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append rows in a python list?

I want to append a row in a python list.

Below is what I am trying,

# Create an empty array
arr=[]
values1 = [32, 748, 125, 458, 987, 361]
arr = np.append(arr, values1)
print arr

[ 32. 748. 125. 458. 987. 361.]

I want to append second row in the list, so that I will get an array like

[ [32. 748. 125. 458. 987. 361.], [42. 344. 145. 448. 187. 304.]]

I am getting error when I try to add second row

values2 = [42, 344, 145, 448, 187, 304]    
arr = np.append(arr, values2)

How to do that?

like image 945
dSb Avatar asked May 08 '26 20:05

dSb


1 Answers

Just append directly to your original list:

# Create an empty list
my_list = []
values1 = [32, 748, 125, 458, 987, 361]
my_list.append(values1)
print(my_list)

values2 = [42, 344, 145, 448, 187, 304]    
my_list.append(values2)
print(my_list)

And this will be your output:

[[32, 748, 125, 458, 987, 361]]
[[32, 748, 125, 458, 987, 361], [42, 344, 145, 448, 187, 304]]

Hope that helps!

like image 143
Hugo Avatar answered May 10 '26 17:05

Hugo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!