Python Forum
problem with list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: problem with list (/thread-11835.html)



problem with list - darktitan - Jul-27-2018

hi
im trying to generat a list from some values but the list ends up kind of strange.

here how i want it to be when i print it

['test-1-1-1', 'test-1-2-2']

and here how it is

['test-1-1-1']
['test-1-1-1', 'test-1-2-2']

my code.

   def funktion(self):
        value1 = (self.a1.get())
        value2 = (self.a2.get())
        value3 = (self.a3.get())
        value4 = (self.a4.get())
        value5 = (self.a5.get())
        self.mylist = []
         
            for parts in range(value3-1, value5):
                self.mylist.append('{}-{}-{}-{}'.format(value1, value2, parts+1, parts+1))
                print (self.mylist)
What am i doing wrong?


RE: problem with list - ichabod801 - Jul-27-2018

Your indentation is wrong, it should be giving you an IndentationError on line 9. Assuming that's just a mistaking in pasting the code, you need to unindent the print statement to be the same level as the for statement. As it is now it is under the for statement, so it's part of the loop and prints every time through the loop. You just want it to print once at the end of the loop, so unindent it once to take it out of the loop.


RE: problem with list - darktitan - Jul-27-2018

yea i noticed that the indentation is wrong on line 9. Its because i modified the code a bit before i posted it and forgot about fixing the Indentation. It hade an if statement before the loop. Your right about the print line. thanks for the help.


RE: problem with list - darktitan - Jul-28-2018

Ok no i got a new problem with the code. when im use append to continue adding to the list it over writes it instead of adding new data. i tryed extend to getting the same problem.

this is how i wanted to print

Quote:test1-1-1-1
test1-1-2-2
test1-1-3-3
test2-2-1-1
test2-2-2-2
test2-2-3-3

instead i get the last added data when i print the list like this.

Quote:test2-2-1-1
test2-2-2-2
test2-2-3-3


this is my code so far.
    def funktion(self):
        value1 = (self.a1.get())
        value2 = (self.a2.get())
        value3 = (self.a3.get())
        value4 = (self.a4.get())
        value5 = (self.a5.get())
        self.mylist = []
        
        for parts in range(value3-1, value5):
            #print('{}-{}-{}-{}\n'.format(value1, value2, parts+1, parts+1))
            #self.text_entry.insert(END, '{}-{}-{}-{}\n'.format(value1, value2, parts+1, parts+1))
            self.mylist.append('{}-{}-{}-{}'.format(value1, value2, parts+1, parts+1))
        print ('\n'.join(self.mylist))

    def file_save(self):
        print ('\n'.join(self.mylist))



RE: problem with list - Axel_Erfurt - Jul-28-2018

an example

value1 = 12
value2 = 13
value3 = 14
value4 = 15
value5 = 16
value6 = 17
mylist = []
mylist.append(value1)
mylist.append(value2)
mylist.append(value3)
mylist.append(value4)
mylist.append(value5)
mylist.append(value6)

for parts in range(2, 5):
    print(mylist[parts])



RE: problem with list - darktitan - Jul-28-2018

Never mind i got it to work. It was a stupid mistake. The list shouldent be in the def funktion(self): every time i called def funktion it reinitialised the list.