How do I declare an array in Python?

I can’t find any reference to arrays in the documentation.

Skip to content
# How to declare an array in Python?

## The Question :

*528 people think this question is useful*
*The Question Comments :*
## The Answer 1

*377 people think this answer is useful*

## The Answer 2

*152 people think this answer is useful*
# Practical answer

# Theoretical answer

## The Answer 3

*128 people think this answer is useful*
## The Answer 4

*66 people think this answer is useful*
## The Answer 5

*38 people think this answer is useful*
## The Answer 6

*24 people think this answer is useful*
## The Answer 7

*19 people think this answer is useful*
## The Answer 8

*19 people think this answer is useful*
## The Answer 9

*16 people think this answer is useful*
## The Answer 10

*13 people think this answer is useful*
## The Answer 11

*11 people think this answer is useful*
## The Answer 12

*8 people think this answer is useful*
## The Answer 13

*6 people think this answer is useful*
## The Answer 14

*5 people think this answer is useful*
## The Answer 15

*4 people think this answer is useful*
## The Answer 16

*2 people think this answer is useful*

2021-01-12

How do I declare an array in Python?

I can’t find any reference to arrays in the documentation.

- For some unfathomable reason, Python calls arrays “lists”. The “everyone-knows-what-this-is-called-so-we’re-going-to-call-it-something-else” school of language design. It’s a particularly bad choice of name, since it looks like a linked list rather than an array.
- @Glenn Maynard: probably because in C-like languages arrays are fixed length while Python lists are not. Its more like STL vector in C++ or ArrayList in Java.
- It’s called a list, because it’s a list. [A(), 1, ‘Foo’, u’öäöäö’, 67L, 5.6]. A list. An array is “an arrangement of items at equally spaced addresses in computer memory” (wikipedia).
- Nothing about the universally-understood term “array” suggests a fixed length or anything about the content; those are just limitations of C’s particular implementation of arrays. Python lists are equally spaced (pointers to objects, internally), or else
`__getitem__`

wouldn’t be O(1). - @Glenn, from en.wikipedia.org/wiki/Array_data_structure : “the elements of an array data structure are required to have the same size” (true for Python’s arrays, not true for Python lists) and “set of valid index tuples and the addresses of the elements (and hence the element addressing formula) are usually fixed while the array is in use” (not true in Python for either list or array).

variable = []

Now `variable`

refers to an empty list^{*}.

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 `array`

module, which offers a type closer to the C `array`

type; the contents must be homogenous (all of the same type), but the length is still dynamic.

This is surprisingly complex topic in Python.

Arrays are represented by class `list`

(see reference and do not mix them with generators).

Check out usage examples:

# empty array arr = [] # init with values (can contain mixed types) arr = [1, "eels"] # get item by index (can be negative to access end of array) arr = [1, 2, 3, 4, 5, 6] arr[0] # 1 arr[-1] # 6 # get length length = len(arr) # supports append and insert arr.append(8) arr.insert(6, 7)

Under the hood Python’s `list`

is a wrapper for a real array which contains references to items. Also, underlying array is created with some extra space.

Consequences of this are:

- random access is really cheap (
`arr[6653]`

is same to`arr[0]`

) `append`

operation is ‘for free’ while some extra space`insert`

operation is expensive

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:

You don’t actually declare things, but this is how you create an array in Python:

from array import array intarray = array('i')

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

f = [] for i in range(30): f.append(0)

An example to where this could be used is in Fibonacci sequence. See problem 2 in Project Euler

This is how:

my_array = [1, 'rebecca', 'allard', 15]

For calculations, use numpy arrays like this:

import numpy as np a = np.ones((3,2)) # a 2D array with 3 rows, 2 columns, filled with ones b = np.array([1,2,3]) # a 1D array initialised using a list [1,2,3] c = np.linspace(2,3,100) # an array with 100 points beteen (and including) 2 and 3 print(a*1.5) # all elements of a times 1.5 print(a.T+b) # b added to the transpose of a

these numpy arrays can be saved and loaded from disk (even compressed) and complex calculations with large amounts of elements are C-like fast.

Much used in scientific environments. See here for more.

JohnMachin’s comment should be the real answer. All the other answers are just workarounds in my opinion! So:

array=[0]*element_count

A couple of contributions suggested that arrays in python are represented by lists. This is incorrect. Python has an independent implementation of `array()`

in the standard library module `array`

“`array.array()`

” hence it is incorrect to confuse the two. Lists are lists in python so be careful with the nomenclature used.

list_01 = [4, 6.2, 7-2j, 'flo', 'cro'] list_01 Out[85]: [4, 6.2, (7-2j), 'flo', 'cro']

There is one very important difference between list and `array.array()`

. While both of these objects are ordered sequences, array.array() is an ordered homogeneous sequences whereas a list is a non-homogeneous sequence.

You don’t declare anything in Python. You just use it. I recommend you start out with something like http://diveintopython.net.

I would normally just do `a = [1,2,3]`

which is actually a `list`

but for `arrays`

look at this formal definition

To add to Lennart’s answer, an array may be created like this:

from array import array float_array = array("f",values)

where *values* can take the form of a tuple, list, or np.array, but not array:

values = [1,2,3] values = (1,2,3) values = np.array([1,2,3],'f') # 'i' will work here too, but if array is 'i' then values have to be int wrong_values = array('f',[1,2,3]) # TypeError: 'array.array' object is not callable

and the output will still be the same:

print(float_array) print(float_array[1]) print(isinstance(float_array[1],float)) # array('f', [1.0, 2.0, 3.0]) # 2.0 # True

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:

values = [1,2,3] int_array = array("i",values) int_array.append(float(1)) # or int_array.extend([float(1)]) # TypeError: integer argument expected, got float

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:

int_array = array('i',[1,2,3]) data = [11,22,33,44,55] sample = [] for i in int_array: sample.append(data[i])

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):

import numpy as np numpy_int_array = np.array([1,2,3],'i') for i in numpy_int_array: print(type(i)) # <class 'numpy.int32'> numpy_int_array_2 = np.append(numpy_int_array,int(1)) # still <class 'numpy.int32'> numpy_float_array = np.append(numpy_int_array,float(1)) # <class 'numpy.float64'> for all values numpy_str_array = np.append(numpy_int_array,"1") # <class 'numpy.str_'> for all values data = [11,22,33,44,55] sample = [] for i in numpy_int_array_2: sample.append(data[i]) # no problem here, but TypeError for the other two

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:

int_numpy_array = np.array([1,2,float(3)],'i') # 3 becomes an int int_numpy_array_2 = np.array([1,2,3.9],'i') # 3.9 gets truncated to 3 (same as int(3.9)) invalid_array = np.array([1,2,"string"],'i') # ValueError: invalid literal for int() with base 10: 'string' # Same error as int('string') str_numpy_array = np.array([1,2,3],'str') print(str_numpy_array) print([type(i) for i in str_numpy_array]) # ['1' '2' '3'] # <class 'numpy.str_'>

or, in essence:

data = [1.2,3.4,5.6] list_1 = np.array(data,'i').tolist() list_2 = [int(i) for i in data] print(list_1 == list_2) # True

while array will simply give:

invalid_array = array([1,2,3.9],'i') # TypeError: integer argument expected, got float

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. 😀

How about this…

>>> a = range(12) >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] >>> a[7] 6

Python calls them lists. You can write a list literal with square brackets and commas:

>>> [6,28,496,8128] [6, 28, 496, 8128]

Following on from Lennart, there’s also numpy which implements homogeneous multi-dimensional arrays.

I had an array of strings and needed an array of the same length of booleans initiated to True. This is what I did

strs = ["Hi","Bye"] bools = [ True for s in strs ]

You can create lists and convert them into arrays or you can create array using numpy module. Below are few examples to illustrate the same. Numpy also makes it easier to work with multi-dimensional arrays.

import numpy as np a = np.array([1, 2, 3, 4]) #For custom inputs a = np.array([int(x) for x in input().split()])

You can also reshape this array into a 2X2 matrix using reshape function which takes in input as the dimensions of the matrix.

mat = a.reshape(2, 2)