I think that you should not use the loop " for value in result" because you are inserting "values= result" in the last command i.e. "tree.insert". Using loop is making the same result get inserted in the table value (no of iterations) no of times.