1

I am trying to reverse my existing array . The reverse elements must insert in array called "new_ar"

My code is not throwing any error but not inserting values also in new array"new_ar"

Existing array is as below - ar = array([1, 2, 3, 4])

i want output as new array which is exactly reverse of above array

i.e new_ar = (4,3,2,1) - > Expected output

i am using below code -

import numpy as np
ar = np.array(list)
len_ar = len(ar)
new_ar = [] ## declaring empty array
x = 0
#print(ar)
#print(len_ar)
while len_ar >0:
   # print(ar[len_ar-1])
    new_ar = np.insert(new_ar,x,ar[len_ar-1])
    len_ar = len_ar -1
    x = x+1
    print(x)
print(new_ar)

i have tried with insert method as well. but no luck.

  • What does not work in your code ? I tried it and it gives an expected result (I changed `list` in line 2 by `[1, 2, 3, 4]`). Why are you using numpy arrays ? But either way you can use `my_list[::-1]` to get the reverse list or numpy array. See https://stackoverflow.com/a/509295/14476967 – ygorg Mar 01 '21 at 13:33
  • The question was to reverse array methaod other than my_list[::-1] . My query was i am able to print the reverse array . but while i am trying to print the new_arr it is appearing empty. – Vikas Lomate Mar 01 '21 at 18:01

0 Answers0