Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
appending list of list
#1
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
Reply
#2
(Mar-29-2020, 05:18 AM)glennford49 Wrote: whats wrong with my code?
apart from mixed indentation?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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.)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,713 Oct-26-2022, 04:03 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,499 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Split a number to list and list sum must be number sunny9495 5 2,195 Apr-28-2022, 09:32 AM
Last Post: Dexty
  How to check if a list is in another list finndude 4 1,791 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  Different out when using conda list and pip list Led_Zeppelin 1 3,966 Jan-14-2022, 09:30 PM
Last Post: snippsat
  Use one list as search key for another list with sublist of list jc4d 4 2,105 Jan-11-2022, 12:10 PM
Last Post: jc4d
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,036 Jan-09-2022, 02:39 AM
Last Post: Python84

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020