title: How to assign new different values to numpy array at the same dimension using iteration function
body: <p>I have the following multidimensional numpy array: </p>
  
  <pre><code>a = np.arange(16).reshape(2,2,2,2)
  </code></pre>
  
  <p>I want to assign new different values of the array for each element of certain dimension e.g. 4th dimension
  I used the following code: </p>
  
  <pre><code>    for i in range(a.shape[3]):
            if i == 0:
                 for t in np.nditer(a[:,:,:,i], op_flags = ['readwrite']):
                      t[...] = t*2
            if i == 1:
                 for t in np.nditer(a[:,:,:,i], op_flags = ['readwrite']):
                      t[...] = t*3
  
  print(a)
  print(a.shape)
  </code></pre>
  
  <p>the output is shown as </p>
  
  <pre><code>[[[[ 0  1]
     [ 4  3]]
  
     [[ 8  5]
     [12  7]]]
  
  
   [[[16  9]
     [20 11]]
  
    [[24 13]
     [28 15]]]]
  [[[[ 0  3]
     [ 4  9]]
  
    [[ 8 15]
     [12 21]]]
  
  
   [[[16 27]
     [20 33]]
  
    [[24 39]
     [28 45]]]]
  (2, 2, 2, 2)
  (2, 2, 2, 2)
  </code></pre>
  
  <p>What I understand that it iterates over the array and at the first <code>i</code> it assigns new values then at the next <code>i</code> it assigns new values and creates new array with both new values besides the first array of the first <code>i</code> that's why I got two arrays in one variable. I only concern about the last array where all values have been assigned the new values. How could I extract the last array only. Or there is another code which is simpler and time saving for this task? </p>