title: Programatically add and remove tkinter python labels causes IndexError: list index out of range
body: <p>Sorry for the vague title but I didn't know how to explain myself better. Basically what I try to do in tkinter here is adding and removing labels. The label value gets updated so that I always have an increment of 1 even though I deleted a label in the beginning. If I generate labels and delete them from the bottom up I have no problems but it I delete one from the middle and then try to clean my list I get an error: </p>
  
  <pre><code>Exception in Tkinter callback
  Traceback (most recent call last):
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
      return self.func(*args)
    File "/Users/XXXX/Helper/development/dynamicListLabels.py", line 21, in &lt;lambda&gt;
      labelList[index].append(ttk.Button(root, text="Remove", command=lambda: removeLabel(labelList[index][0], index)))
  IndexError: list index out of range
  </code></pre>
  
  <p>My python code looks like this:</p>
  
  <pre><code>#!/usr/bin/python
  from Tkinter import *
  import ttk
  
  
  def removeLabel(labelToRemove, bla):
      labelList[labelToRemove.get()][1].destroy()
      labelList[labelToRemove.get()][2].destroy()
      del labelList[labelToRemove.get()]
      for label in labelList:
          index = labelList.index(label)
          label[0].set(index)
  
  
  def addNewLabel():
      labelList.append([IntVar()])
      index = len(labelList) - 1
      labelList[index][0].set(index)
      labelList[index].append(ttk.Label(root, textvariable=labelList[index][0]))
      labelList[index].append(ttk.Button(root, text="Remove", command=lambda: removeLabel(labelList[index][0], index)))
      labelList[index][1].grid(column=0)
      labelList[index][2].grid(column=1, row=labelList[index][1].grid_info()['row'])
  
  
  root = Tk()
  labelList = []
  ttk.Button(root, text="add label", command=addNewLabel).grid(column=1, row=0)
  root.mainloop()
  </code></pre>
  
  <p>And my GUI looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/p3YhP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p3YhP.png" alt="enter image description here"></a></p>
  
  <p>Thanks for your help!</p>