arrays in python

Simon Forman sajmikins at gmail.com
Thu Sep 24 05:22:15 CEST 2009


On Wed, Sep 23, 2009 at 10:03 PM, AggieDan04 <danb_83 at yahoo.com> wrote:
> On Sep 23, 3:02 pm, Simon Forman <sajmik... at gmail.com> wrote:
>> On Wed, Sep 23, 2009 at 1:14 PM, Rudolf <yellowblueyel... at gmail.com> wrote:
>> > Can someone tell me how to allocate single and multidimensional arrays
>> > in python. I looked online and it says to do the following x =
>> > ['1','2','3','4']
>>
>> > However, I want a much larger array like a 100 elements, so I cant
>> > possibly do that. I want to allocate an array and then populate it
>> > using a for loop. Thanks for your help.
>> > --
>> >http://mail.python.org/mailman/listinfo/python-list
>>
>> In python they're called 'lists'.  There are C-style array objects but
>> you don't want to use them unless you specifically have to.
> ...
>> But if you do this:
>>
>> two_dimensional_list = [ [] for var in some_iterable]
>>
>> The list of lists you create thereby will contain multiple references
>> to the /same/ inner list object.
>
> No, that creates a list of distinct empty lists.  If you want multiple
> references to the same inner list, it's
>
> inner_list = []
> two_dimensional_list = [inner_list for var in some_iterable]
>
> or
>
> two_dimensional_list = [[]] * num_copies
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Oh, you're right.  I was thinking of the [[]] * n form.  My bad.

~Simon



More information about the Python-list mailing list