446,213 Members | 1,086 Online
Need help? Post your question and get tips & solutions from a community of 446,213 IT Pros & Developers. It's quick & easy.

# Define a 2d Array?

 P: n/a Hi. How do I define a 2d list? For instance, to define a 4 by 5 list, I wanted to do this: n=4 m=5 world = [n][m] However, it gives me an invalid syntax error saying the index is out of range. I know this is a real newbie question, but if you could help me out, I'd really appreciate it! Thanks, Jill Oct 12 '08 #1
6 Replies

 P: n/a JillHow do I define a 2d list? Python doesn't truly have 2d lists in the way you might think of 2d arrays in C or Fortran. It has 1d lists which can contain any Python object, including other lists. If you wanted to create a 4x5 list you'd do something like this: N = 4 M = 5 mylist = [] for i in range(N): mylist.append([0.0] * M) If you are looking to do numeric work with such multidimensional lists you should consider the builtin array object or the numpy package: http://docs.python.org/dev/library/a...l#module-array http://numpy.scipy.org/ Skip Oct 12 '08 #2

 P: n/a On Oct 11, 9:30*pm, s...@pobox.com wrote: * * JillHow do I define a 2d list? Python doesn't truly have 2d lists in the way you might think of 2d arrays in C or Fortran. *It has 1d lists which can contain any Python object, including other lists. *If you wanted to create a 4x5 list you'd do something like this: * * N = 4 * * M = 5 * * mylist = [] * * for i in range(N): * * * * mylist.append([0.0] * M) If you are looking to do numeric work with such multidimensional lists you should consider the builtin array object or the numpy package: * *http://docs.python.org/dev/library/a...l#module-array * *http://numpy.scipy.org/ Skip I think you can do mylist = [[]] or somesuch... if you are looking on google for examples you will comonly find them in spreadsheets.. I have one in the editor part of dex tracker (available on source forge) The array will start at zero and ie x[0] and will keep growing as long as you .append it.. You don't define the size in advance like you would with other languages.. You need to have values befour you try to use a location in the array. Oct 12 '08 #3

 P: n/a On Oct 12, 1:30*pm, s...@pobox.com wrote: * * JillHow do I define a 2d list? > If you are looking to do numeric work with such multidimensional lists you should consider the builtin array object or the numpy package: * *http://docs.python.org/dev/library/a...l#module-array The built-in array module does *NOT* support multidimensional arrays. The referenced docs say (first two sentences) "This module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers. Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained." "behave very much like lists" is in no way to be construed as supporting multiple dimensions, and that type constraint means that you can't even have an array of arrays. However you can have a list of arrays; this can be a memory-efficient solution for some 2D applications. Note that arrays are not recommended for CPU efficiency, as (in general) each time you access an array element, a new Python object must be created. Escape clause: CPython and single-byte arrays (type 'c' and some values of types 'b' and 'B'). Cheers, John Oct 12 '08 #4

 P: n/a EricI think you can do Ericmylist = [[]] or somesuch... That won't do what you want. You've defined a list with a single element (another list). You might have been thinking of something like this: >>N = 4M = 5mylist = [[0.0] * M] * N While to the casual glance it looks like what you want: >>print mylist [[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0]] assigning to an element of this structure demonstrates its shortcoming: >>mylist[1][1] = 42.7print mylist [[0.0, 42.700000000000003, 0.0, 0.0, 0.0], [0.0, 42.700000000000003, 0.0, 0.0, 0.0], [0.0, 42.700000000000003, 0.0, 0.0, 0.0], [0.0, 42.700000000000003, 0.0, 0.0, 0.0]] There is just one copy of [0.0] * M referenced N times. Skip Oct 12 '08 #5

 P: n/a On Oct 11, 9:19*pm, Jillian Calderon wrote: How do I define a 2d list? For instance, to define a 4 by 5 list, I wanted to do this: n=4 m=5 world = [n][m] However, it gives me an invalid syntax error saying the index is out of range. Here are some examples of how you can use list comprehensions to do this: In [1]: n=4 In [2]: m=5 In [3]: world = [[[] for ni in range(n)] for mi in range(m)] In [4]: world Out[4]: [[[], [], [], []], [[], [], [], []], [[], [], [], []], [[], [], [], []], [[], [], [], []]] In [5]: world[0][0] Out[5]: [] In [6]: len(world) Out[6]: 5 In [7]: len(world[0]) Out[7]: 4 In [8]: world = [[[ni+mi*n] for ni in range(n)] for mi in range(m)] In [9]: world Out[9]: [[[0], [1], [2], [3]], [[4], [5], [6], [7]], [[8], [9], [10], [11]], [[12], [13], [14], [15]], [[16], [17], [18], [19]]] Best, Tom Oct 13 '08 #6

 P: n/a Thanks, everyone. All of your help solved at least my data storage problem! Oct 13 '08 #7

### This discussion thread is closed

Replies have been disabled for this discussion.