[Tutor] A shorter way to initialize a list?

Kaixi Luo kaixiluo at gmail.com
Tue Dec 13 21:39:08 CET 2011


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'm learning Python coming from a C++ background. So far, I've found that
Python is an amazingly concise and expressive language. I just want to make
sure I'm not being badly influenced by my old C++ habits.

Cheers,

Kaixi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111213/63d5958e/attachment.html>


More information about the Tutor mailing list