0

I have to write a function that returns a reversed Numpy Array with element type float from a list.

>>>myList = [1, 2, 3, 4, 5]
>>>myList
[1, 2, 3, 4, 5]

I used the reverse() function while passing the list to the numpy.array() function like so:

>>>def arrays(someList):
       return numpy.array(someList.reverse(), float)

But this returns an empty array:

>>>arrays(myList)
array(nan)

However, if I reverse the list using reverse() before passing it, it works alright.

>>>def arrays(someList):
       someList.reverse()
       return numpy.array(someList, float)

>>>arrays(myList)
array([5.,  4.,   3.,   2.,   1.])

On the contrary, if I use the list[::-1] technique to reverse the list in the same place as I had used the reverse() function the first time, it works like a charm.

>>>def arrays(someList):
       return numpy.array(someList[::-1] , float)

>>>arrays(myList)
array([5.,  4.,   3.,   2.,   1.])

Although my code works alright, I want to understand why the first way of using reverse() doesn't work with the numpy.array() function.

Thanks!

Edit 1: Sorry I meant numpy.array(someList.reverse(), float) and not `numpy.array(someList, reverse(), float)

  • Did you mean to write `return numpy.array(someList.reverse(), float)`? – Barmar Jul 14 '18 at 06:40
  • What `reverse()` function? There's no `reverse` either built-in or in NumPy. Where's the `reverse()` in `numpy.array(someList, reverse(), float)` supposed to be coming from? – user2357112 Jul 14 '18 at 06:44
  • Sorry I meant `someList.reverse()` and not `someList, reverse()`. I've made the edit. – Vimugdh Lall Jul 14 '18 at 07:01
  • Thank you @Barmar for pointing me in the right direction. I somehow couldn't find that question when I was searching prior to posting my question. – Vimugdh Lall Jul 14 '18 at 07:02

0 Answers0