3

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?

1 Answer 1

12

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.