From: tutor-bounces+ramit.prasad=jpmorgan....@python.org [mailto:tutor-bounces+ramit.prasad=jpmorgan....@python.org] On Behalf Of Kaixi Luo Sent: Tuesday, December 13, 2011 2:39 PM To: tutor@python.org Subject: [Tutor] A shorter way to initialize a list?
Hello, I want to create a list of lists of lists (listB) from a list of lists (listA). Below there's a snippet of my code: list1 = [[] for i in range(9)] # some code here... listA = [[] for i in range(3)] count = 0 for i in range(3): for j in range(3): listB[i].append(listA[count]) count+=1 My question is: is there any alternative way I can initialize listB without resorting to using 2 loops? ======================================== I do not understand the double loop. listA is only of length 3, so it will fail as soon as count=3 (when i=1,j=0). listA only has 3 sublists and listB has 9. I am not sure how you are trying to append them and frankly I can guess half a dozen various reasons that seem equally plausible to me. If they are both supposed to be the same size then you should check copy.deepcopy which will make new objects (versus just references) for every sublist (or any object for that matter ) in the original list. Otherwise you will need to be aware that appending a sublist from listA to listB will mean that if you modify a sublist in listA it will modify the corresponding sublist of listB and vice-versa. If you clarified what you are trying to do (or better yet gave sample results), I could help you better. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor