1

I want to initialize an empty list and keep on adding new rows to it. For example. myarray=[] now at each iteration I want to add new row which I compute during iteration. For example

for i in range(5):
     calc=[i,i+1,i+4,i+5]

After calc I want to add this row to myarray. Therfore after 1st iteration myarray would be 1X4, after 2nd iteration it would be 2X4 etc. I tried numpy.concatenate. It simply adds to same row ie I get 1X4 then 1X8. I tried vstack as well but since myarray is initially [] it gives error "all the input array dimensions except for the concatenation axis must match exactly"

3
  • 1
    Why don't you just do myarray.append(calc)? Commented Jul 13, 2015 at 14:03
  • 1
    it appends to same row as I mentioned. In next iteration it gives 1X8 in next it gives 1X12 and so on. I want 1X4 then 2X4 then 3X4 and so on Commented Jul 13, 2015 at 14:05
  • It does not append to the same row. It adds a new row. Commented Jul 13, 2015 at 14:11

2 Answers 2

1

It looks like you need a multi dimensional array

calc = [[0, 1, 4, 5]]
for i in range(1, 5):
    calc.append([i, i+1, i+4, i+5])

Will yield you the following array

calc = [[0, 1, 4, 5], [1, 2, 5, 6], [2, 3, 6, 7], [3, 4, 7, 8], [4, 5, 8, 9]]

To access the various elements of calc you can address it like the following

calc[0] returns [0,1,5,6]
calc[1] returns [1,2,5,6]
Sign up to request clarification or add additional context in comments.

2 Comments

It looks like I am unable to understand difference between numpy arrays and list in python. Pardon me I just switched from matlab to python. What difference will it make if I am using numpy?
Python lists are bit like MATLAB cell arrays; numpy arrays are like the regular MATLAB matrix (except they aren't always 2d). append is natural for lists. I discourage using append with arrays.
0

I'm pretty sure this works, unless I'm misunderstanding:

mylist = [] #I'm using a list, not an array
for i in range(5):
     calc=[i,i+1,i+4,i+5]
     mylist.append(calc) #You're appending a list into another list, making a nested list

Now, a little more general knowledge. Append vs. Concatenate.

You want to append if you want to add into a list. In this case, you're adding a list into another list. You want to concatenate if you want to 'merge' two lists together to make a single list - which is why your implementation was not making a nested list.

3 Comments

Hey thanks. I got the difference between append and concatenate. It looks like I missed difference between python list and numpy arrays. Pardon me I just switched from matlab to python. What difference will it make if I am using numpy?
There's not much difference between the two. Anything you can do with a list, you can do to with numpy array. I've never tested this out, but the general consensus seems to be that numpy is better for reading and writing with less overhead. If you're adding many elements into a list (thousands and thousands), I would suggest numpy. Otherwise, I would stick with using a normal list.
this is wrong. numpy creates a new array each time you concatenate/append values. depending on what you want to do/space constraints you can just use python lists and then convert to numpy at the end.

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.