2

I have the following problem: I create an array and then I turned it to a list, to which I want to add two more values. I have tried with append and with insert but I becoma the error message: 'NoneType' object has no attribute 'insert'. That means that my list is not a list. Here is what I am trying to do:

f = 25
bi = np.arange(-f, f + 5, 5)
beta = bi.tolist()
print "beta:", beta

d = np.arange(-f, f + 5, f / 3)
di = d.tolist()
print "di:", di

dj = di.insert(1, -f / 2)
print "dj:", dj 

dk = dj.insert(5, f / 2)
dw = sorted(dk)
delta = [round(elem, 0) for elem in dw]
print "delta:", delta   

Has anyone an idea what I am doing wrong or how can Imake it work? Moreover the "sorted" seems also not to be working.

1
  • 1
    always add full error message. Commented Jan 20, 2016 at 14:32

4 Answers 4

1

Problem is on 9th line: list insert method does not return anything (means None),

dj=di.insert(1,-f/2)

so dj will get assigned None so this statement will raise an error.

dk=dj.insert(5,f/2)

Now Try This:

f=25
bi=np.arange(-f,f+5,5)
beta=bi.tolist()
print "beta:", beta
d=np.arange(-f,f+5,f/3)
di=d.tolist()
print "di:", di
di.insert(1,-f/2)
print "di:", di
di.insert(5,f/2)
dw=sorted(di)
delta=[round(elem, 0) for elem in dw]
print "delta:", delta
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That works. Although I don't get why in dj is assigned NONE. Is assigned di.insert.
1

insert() and append() doesnt't return new list so you can't assign it to dj

di.insert(1,-f/2)

print "di:", di

di.insert(5,f/2)

print "di:", di

dw = sorted(di)

Comments

0

Your problem is in these lines:

dj=di.insert(1,-f/2)
dk=dj.insert(5,f/2)

di.insert() inserts something into the list di and returns None, so you're assigning None to dj.

THEN, you're trying to call the function insert() in the 'value' None which doesn't have it so you're getting an error.

Comments

0

Yeah that's happening because di.insert() doesn't actually return anything and that's why dj is a NoneType object.

Do this instead

from copy import copy
dj=copy(di)
dj.insert(1,-f/2)

2 Comments

dj and di will be names for the same object.
Yeah that's why he needs a copy of the list. copy.copy should work. Provided OP wants a new copy of the list for whatever he's doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.