Re: arrays in python

Discussion in 'Python' started by Simon Forman, Sep 23, 2009.

  1. Simon Forman

    Simon Forman Guest

    On Wed, Sep 23, 2009 at 1:14 PM, Rudolf <> 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.

    You can create an empty list like so:

    x = []

    and then put items into it with a for loop like so:

    for item in some_iterable:
    x.append(item)

    But in simple cases like this there's also the "list comprehension"
    syntax which will do it all in one step:

    x = [item for item in some_iterable]

    But again in a case like this, if you're simply populating a list from
    an iterable source you would just say:

    x = list(some_iterable)

    For multidimensional 'arrays' you just put lists in lists. (Or use
    NumPy if you really want arrays and are doing lots of wild processing
    on them.)

    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. This is something that catches many
    people unawares. You have to go back to using a for loop:

    x = []
    for n in range(100):
    x.append([(n, m) for m in range(10)])

    (That will create a list of one hundred lists, each of which contains
    ten tuples.)

    Good luck.
    Simon Forman, Sep 23, 2009
    #1
    1. Advertising

  2. Simon Forman

    AggieDan04 Guest

    On Sep 23, 3:02 pm, Simon Forman <> wrote:
    > On Wed, Sep 23, 2009 at 1:14 PM, Rudolf <> 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
    AggieDan04, Sep 24, 2009
    #2
    1. Advertising

  3. Simon Forman

    Simon Forman Guest

    On Wed, Sep 23, 2009 at 10:03 PM, AggieDan04 <> wrote:
    > On Sep 23, 3:02 pm, Simon Forman <> wrote:
    >> On Wed, Sep 23, 2009 at 1:14 PM, Rudolf <> 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
    Simon Forman, Sep 24, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,506
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,215
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    547
  4. Philipp
    Replies:
    21
    Views:
    1,093
    Philipp
    Jan 20, 2009
  5. Javier Montoya

    numpy arrays to python compatible arrays

    Javier Montoya, Jun 10, 2010, in forum: Python
    Replies:
    3
    Views:
    437
    Javier Montoya
    Jun 12, 2010
Loading...

Share This Page