Charles Charles - 1 month ago 5
Python Question

Python printing "<built-in method ... object" instead of list

import numpy as np
arr = list(map(float,input().split()))
print(np.array(arr.reverse))


Why is this printing this instead of the contents of the list?

# outputs "<built-in method reverse of list object at 0x107eeeec8>"

Answer

You have two problems.

The first problem is that you are not actually calling the reverse method on your array arr.

You have this: arr.reverse

You have to actually call it -> arr.reverse()

Simple example below:

>>> [1,2,3].reverse
<built-in method reverse of list object at 0x100662c68>

Without calling reverse, the output you get is the uncalled reverse method of the list object. Which is very similar to the output you were getting.

The second problem you have is that reverse() method does the reverse in place, which means it performs the reverse on arr (your arr will be reversed) and returns None. So, when you are passing this:

np.array(arr.reverse())

You are returning the return of arr.reverse() to your np.array call, which is None.

So, fixing those two items, by calling arr.reverse() on its on, and then passing arr, will give you the result you are expecting:

import numpy as np
arr = list(map(float,input().split()))
arr.reverse()
res = np.array(arr)
print(res)

Demo:

1 2 3 4
[ 4.  3.  2.  1.]
Comments