[Tutor] A shorter way to initialize a list?

Kaixi Luo kaixiluo at gmail.com
Wed Dec 14 00:26:02 CET 2011


On Tue, Dec 13, 2011 at 10:09 PM, Emile van Sebille <emile at fenx.com> wrote:

> On 12/13/2011 12:39 PM Kaixi Luo said...
>
>  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)]
>>
>
> Should this be listB?
>
>
>
>> # 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])
>>
>
> ... if not, where's this come from?
>
> Emile
>
>
>          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
>>
>>
>> ______________________________**_________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>>
>
>
> ______________________________**_________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>

Uh, sorry. I made some very bad typos there. It should be:

listA = [[] for i in range(9)]

# some code here...

listB = [[] for i in range(3)]
count = 0
for i in range(3):
    for j in range(3):
        listB[i].append(listA[count])
        count+=1


Kaixi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111214/6d9ff0ae/attachment-0001.html>


More information about the Tutor mailing list