Python Forum
appending list of 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: appending list of list (/thread-25385.html)



appending list of list - glennford49 - Mar-29-2020

whats wrong with my code?
i cant get the desired output

list1=[]
list2=[]
num=[1,2,3,4,5,6]
while(True):
    if (len(list2)<=2): # generating numbers
            n = random.choice(num)
            list1.append(n)
            list1.sort()
            num.remove(n)
            if(len(list1)==3): # appending to list if it has 3 elements
                list2.append(list1)
                num=[1,2,3,4,5,6] #declaring num list again
            
    else:
            print("no")
            break
            

print("list2",list2)# printing 3 elements in format [[x,x,x],[x,x,x],[x,x,x]]  <=== desired output



RE: appending list of list - buran - Mar-29-2020

(Mar-29-2020, 05:18 AM)glennford49 Wrote: whats wrong with my code?
apart from mixed indentation?


RE: appending list of list - ibreeden - Mar-29-2020

(Mar-29-2020, 05:18 AM)glennford49 Wrote: i cant get the desired output
... And you fail to tell us what you get instead of the desired output. That makes it more difficult for us to see what goes wrong.
When you get output you do not understand you had best add print statements to your code to see step by step what is really happening. You may add print statements to show the value of num, list1 and list2 just before or after you change them.
(Hint: concentrate on list1.)