How do I declare an array in Python?
I can't find any reference to arrays in the documentation.
Now Of course this is an assignment, not a declaration. There's no way to say in Python "this variable should never refer to anything other than a list", since Python is dynamically typed. *The default built-in Python type is called a list, not an array. It is an ordered container of arbitrary length that can hold a heterogenous collection of objects (their types do not matter and can be freely mixed). This should not be confused with the |
|||||||||||||||||||||
|
You don't actually declare things, but this is how you create an array in Python:
For more info see the array module: http://docs.python.org/library/array.html Now possible you don't want an array, but a list, but others have answered that already. :) |
|||||||||||||||||
|
I think you (meant)want an list with the first 30 cells already filled. So
An example to where this could be used is in Fibonacci sequence. See problem 2 in Project Euler |
|||||||||||||
|
You don't declare anything in Python. You just use it. I recommend you start out with something like http://diveintopython.net. |
|||||||||||||
|
This is how:
|
||||
|
I would normally just do |
|||
|
for calculations, use numpy arrays like this:
these numpy arrays can be saved and loaded from disk (even compressed) and complex calculations with large amounts of elements is C-like fast. Much used in scientific environments. See here for more... |
|||
|
This is surprisingly complex topic in Python. Practical answerArrays are represented by class Check out usage examples:
Theoretical answerUnder the hood Python's Consequences of this are:
Check this awesome table of operations complexity. Also, please see this picture, where I've tried to show most important differences between array, array of references and linked list:
|
|||||||||
|
Following on from Lennart, there's also numpy which implements homogeneous multi-dimensional arrays. |
|||
|
How about this...
|
||||
|
To add to Lennart's answer, an array may be created like this:
where values can take the form of a tuple, list, or np.array, but not array:
and the output will still be the same:
Most methods for list work with array as well, common ones being pop(), extend(), and append(). Judging from the answers and comments, it appears that the array data structure isn't that popular. I like it though, the same way as one might prefer a tuple over a list. The array structure has stricter rules than a list or np.array, and this can reduce errors and make debugging easier, especially when working with numerical data. Attempts to insert/append a float to an int array will throw a TypeError:
Keeping values which are meant to be integers (e.g. list of indices) in the array form may therefore prevent a "TypeError: list indices must be integers, not float", since arrays can be iterated over, similar to np.array and lists:
Annoyingly, appending an int to a float array will cause the int to become a float, without throwing an exception. np.array retain the same data type for its entries too, but instead of giving an error it will change its data type to fit new entries (usually to double or str):
This is true during assignment as well. If the data type is specified, np.array will, wherever possible, transform the entries to that data type:
or, in essence:
while array will simply give:
Because of this, it is not a good idea to use np.array for type-specific commands. The array structure is useful here. list preserves the data type of the values. And for something I find rather pesky: the data type is specified as the first argument in array(), but (usually) the second in np.array(). :| The relation to C is referred to here: Python List vs. Array - when to use? Have fun exploring! Note: The typed and rather strict nature of array leans more towards C rather than Python, and by design Python does not have many type-specific constraints in its functions. Its unpopularity also creates a positive feedback in collaborative work, and replacing it mostly involves an additional [int(x) for x in file]. It is therefore entirely viable and reasonable to ignore the existence of array. It shouldn't hinder most of us in any way. :D |
||||
|
Python calls them lists. You can write a list literal with square brackets and commas:
|
|||
|
A couple of contributions suggested that arrays in python are represented by lists. Perhaps theoretically/under the hood that is correct however a major distinction between the two is the fact that lists accept mixed data types and mixed numeric types, on the other hand array requires a type-code restricting all elements to the determined type:
This is not possible using array(). |
||||
|
I had an array of strings and needed an array of the same length of booleans initiated to True. This is what I did
|
|||
|
__getitem__
wouldn't be O(1). – Glenn Maynard Oct 3 '09 at 20:13