'NoneType' object has no attribute 'insert' Python List append insert

15,943

Solution 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

Solution 2

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)
Share:
15,943
GeMa
Author by

GeMa

Updated on June 04, 2022

Comments

  • GeMa
    GeMa almost 2 years

    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.