Python Forum

Full Version: appending list of list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
(Mar-29-2020, 05:18 AM)glennford49 Wrote: [ -> ]whats wrong with my code?
apart from mixed indentation?
(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.)