Search code examples
pythoninsertattributeerror

AttributeError: 'int' object has no attribute 'insert'


HELP! Just when I get one thing working, something else doesn't work! Again, I'm sure it's simple to experienced eyes, but I'm struggling! This is the code where I generate my lists and the data for said lists.

#Frame Creation


allframes = []

for n in range (0, (workingframes*archnodes*3)):
    allframes.append(n)

frames = allframes

print frames



#Frame Population

for f in range (0, workingframes):

    if f<=(workingframes/2):

        for x in range (0, (archnodes)):
            frames[((archnodes*3)+f)].insert(((archnodes*3)+f), (archstartred[x]))
            frames[((archnodes*3)+f+workingframes)].insert(((archnodes*3)+f+workingframes),(archstartgrn[x]))
            frames[((archnodes*3)+f+workingframes*2)].insert(((archnodes*3)+f+workingframes*2),(archstartblu[x]))

        for y in range (0, nodesperframe):
            archstartred.pop()
            archstartgrn.pop()
            archstartblu.pop()
            archstartred.insert(0, backred)
            archstartgrn.insert(0, backgrn)
            archstartblu.insert(0, backblu)

    else:
        for y in range (0, nodesperframe):
            archstartred.pop(0)
            archstartgrn.pop(0)
            archstartblu.pop(0)
            archstartred.append(backred)
            archstartgrn.append(backgrn)
            archstartblu.append(backblu)

        for x in range (0, (archnodes)):
            frames[(archnodes*3)+f].insert((archnodes*3), (archstartred[x]))
            frames[(archnodes*3)+f+workingframes].insert(((archnodes*3)+1),(archstartgrn[x]))
            frames[(archnodes*3)+f+workingframes*2].insert(((archnodes*3)+2),(archstartblu[x]))

I keep getting this lovely error:

AttributeError: 'int' object has no attribute 'insert'

I plan on converting the list to an array after the list is populated so that I can have it manipulated the way it needs to be. I'm sure there's a much easier way to do this, but I don't know and I'm new to Python!


Solution

  • These expressions frames[((archnodes*3)+f)] (all 3 versions) evaluate to be integers and you are trying to call an insert() method on them which is the cause of your error.

    f is an integer created in the enclosing for loop, and the way archnodes are used in the first for loop also implies they are numbers. frames is a list of numbers too created in your first loop, so you are indexing into a list of numbers and then trying to call the insert() method on the specific number in the list. As the error message states,

    'int' object has no attribute 'insert'

    the type int does not have an insert method.

    Update re comment above:

    You can add/remove/insert items to the list, but you can't add/remove/insert to an integer. See this brief tutorial/reference on lists. So for instance to insert an item into a list you'd use this methods/approach:

    list.insert(index, obj)
    

    so in your case

    frames.insert(index, .. )
    

    not

    frames[index].insert(..)
    

    I hope this helps.