Home > other >  why doesnt numpy create an array when executing a list method
why doesnt numpy create an array when executing a list method

Time:12-04

playing around with numpy:

import numpy as np
l = [39, 54, 72, 46, 89, 53, 96, 64, 2, 75]
nl = np.array(l.append(3))


>> array(None, dtype=object)

now, if i call on l, i'll get the new list: [39, 54, 72, 46, 89, 53, 96, 64, 2, 75, 3]

my question is, why doesn't numpy create that list as an array?

if i do something like this:

nl = np.array(l.extend([45])) i get the same thing.

but if try to concatenate without a method: nl = np.array(l [45]) it works.

so, is it because numpy is trying to create the array before the method is called? or what?

CodePudding user response:

The append function will always return None. You must do this in two different lines of code:

import numpy as np
l = [39, 54, 72, 46, 89, 53, 96, 64, 2, 75]
l.append(3)
nl = np.array(l)

CodePudding user response:

append and extend are in-place methods and return None.

print(l.append(3)) # None
print(l.extend([3])) # None
  • Related